The 4 types of Throwable constructors
are :-
Throwable() : Constructs a new throwable with null as its detail message.
Throwable(String message) : Constructs a new throwable with the specified detail message.
Throwable(String message, Throwable cause) : Constructs a new throwable with the specified detail message and cause.
Throwable(Throwable cause) : Constructs a new throwable with the specified cause and a detail message of (cause==null ? null : cause.toString())
In a piece of code, the first two constructors types are working properly, but the other two are reporting compile time error.
IOException e = new IOException(); //Working properly
ArithmeticException ae = new ArithmeticException("Top Layer"); //Working properly
ArithmeticException ae = new ArithmeticException("Top Layer", e); //Not working
ArithmeticException ae = new ArithmeticException(e); //Not working
The last two declarations are reporting error
No suitable constructors found for ArithmeticException
I am using JDK 8
Why the last two declarations are reporting error? Also how do I make them work?
Because
ArithmeticException
is a unchecked exceptions and from RuntimeExceptionFrom ArithmeticException
there is no constructor as below thats why its giving you compile time error:
Better use RuntimeException for this: