What class should you extend in order to create your own exception that you can throw?

Java has many built-in exception classes, such as NullPointerException and IllegalArgumentException. At times however, you might want to create your own exception class. For example, as opposed to throwing IllegalArgumentException when a 0 is detected as a divisor during a division operation, you might wish to throw a DivideByZeroException. This exception class does not exist in the Java core API, but you can create one yourself. The seven steps below will show you how to create an exception class in Java.

  1. First, you will create the custom exception class. Open your text editor and type in the following Java statements:
    What class should you extend in order to create your own exception that you can throw?
    The class extends the Exception class that is defined in the Java core API (in the package is java.lang). When extending Exception you are defining a "checked" exception, i.e., an exception that must be caught or thrown. A constructor is provided that passes the message argument to the super class Exception. The Exception class supports a message property.
  2. Save your file as DivideByZeroException.java.
  3. Open a command prompt and navigate to the directory containing your Java program. Then type in the command to compile the source and hit Enter.
    What class should you extend in order to create your own exception that you can throw?
  4. Now you will create the program to test your new exception class. Open your text editor and type in the following Java statements:
    What class should you extend in order to create your own exception that you can throw?
    Notice that the divideInt method must provide a throw clause because the method potentially throws the DivideByZeroException. To create the exception object, the program uses the throw keyword followed by the instantiation of the exception object. At runtime, the throw clause will terminate execution of the method and pass the exception to the calling method.
  5. Save your file as TestDivideByZeroException.java.
  6. Open a command prompt and navigate to the directory containing your Java program. Then type in the command to compile the source and hit Enter.
    What class should you extend in order to create your own exception that you can throw?
  7. Type in the command to run your program and hit Enter.
    What class should you extend in order to create your own exception that you can throw?
    The first call to the divideInt method is successful. The second call, using a divisor of 0, causes the DivideByZeroException to be thrown in the divideInt method. The message passed to the constructor is displayed in the output.

I am designing a custom Exception class for my application. I have a very basic question. Should I extend from Exception class or Thowable class ? What are the benefits ?

I intend to throw this from underlying layers and catch it in the top level classes. Will it influence my decision of using Thowable over Exception. Is it fundamentally right to catch a Thowable ?

I 've gone through some other threads in this forum. They talk about having the stack trace maintained when it is thrown and not having it for exception etc. I understand that some say ( here) that Thowable is super class of Exception and we should not use it. But others (here) say Exception is for "Exceptional " cases.

This question is rather a discussion of how one is better than other rather than asking how.

asked Feb 15, 2013 at 9:34

2

Throwable is a class for all the bad situations, which can arise: Errors & Exceptions.

Error is something, you can't handle at all: OutOfMemoryError, VirtualMachineError, etc.

Exception is for exceptional cases.

Exceptions come in 2 flavours:

  1. RuntimeExceptions.

    These ones, you are not aware of: NullPointerException, ClassCastException, etc.

  2. Checked exceptions.

    These are the exceptions, which your code is aware of and should be explicitely catched (... throws MyException): IOExceptions, etc.

If you want the users of your code, to explicitely handle some exceptional situations, it would be good to just extend Exception, not the RuntimeException. There's no need to extend Throwable.

answered Feb 15, 2013 at 9:49

Ostap AndrusivOstap Andrusiv

4,7071 gold badge35 silver badges38 bronze badges

Throwable is the super class of Error & Exception.

Like Exception, Error too, can be thrown & handled.

But it is not advisable, according to the following doc:

You are not required to catch Error objects or Error subtypes. You can also throw an Error yourself (although other than AssertionError you probably won't ever want to), and you can catch one, but again, you probably won't. What, for example, would you actually do if you got an OutOfMemoryError?

Keeping this concept in mind, I would suggest to extend Throwable if you want to throw and/or catch Exception & Error both. Extend Exception if you want to throw and/or catch Exception only.

answered Feb 15, 2013 at 9:51

RASRAS

8,04516 gold badges64 silver badges86 bronze badges

0

Fundamentally you should extends Exception class as you are creating Custom Exception. Exception and Error both extends Throwable, It really does not make sense extending Throwable.

answered Feb 15, 2013 at 9:40

What class should you extend in order to create your own exception that you can throw?

Can exception class be extended?

The class extends the Exception class that is defined in the Java core API (in the package is java. lang ). When extending Exception you are defining a "checked" exception, i.e., an exception that must be caught or thrown. A constructor is provided that passes the message argument to the super class Exception .

How do you create and throw exception?

We can also define our own set of conditions and throw an exception explicitly using throw keyword. For example, we can throw ArithmeticException if we divide a number by another number. Here, we just need to set the condition and throw exception using throw keyword. The syntax of the Java throw keyword is given below.

Can I throw my own exception in Java?

You can create your own exceptions in Java. All exceptions must be a child of Throwable. If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the Exception class. If you want to write a runtime exception, you need to extend the RuntimeException class.

What exception can be to throw Java?

There are mainly two types of exceptions in Java as follows: Checked exception. Unchecked exception.