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.
- First, you will create
the custom exception class. Open your text editor and type in the following Java statements:The class extends the
Exception
class that is defined in the Java core API [in the package isjava.lang
]. When extendingException
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 classException
. TheException
class supports amessage
property. - Save your file as DivideByZeroException.java.
- 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.
- Now you will create the program to test your new exception class. Open your text
editor and type in the following Java statements:Notice that the
divideInt
method must provide athrow
clause because the method potentially throws theDivideByZeroException
. To create the exception object, the program uses thethrow
keyword followed by the instantiation of the exception object. At runtime, thethrow
clause will terminate execution of the method and pass the exception to the calling method. - Save your file as TestDivideByZeroException.java.
- 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.
- Type in the command to run your program and hit
Enter.The first call to the
divideInt
method is successful. The second call, using a divisor of 0, causes theDivideByZeroException
to be thrown in thedivideInt
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:
RuntimeException
s.These ones, you are not aware of:
NullPointerException
,ClassCastException
, etc.Checked
exceptions.These are the exceptions, which your code is aware of and should be explicitely catched [
... throws MyException
]:IOException
s, 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