I’ve encountered a class which extends Exception :
public class MyException extends Exception
{
public MyException()
{
super();
}
public MyException(final String argMessage, final Throwable argCause)
{
super(argMessage, argCause);
}
public MyException(final String argMessage)
{
super(argMessage);
}
public MyException(final Throwable argCause)
{
super(argCause);
}
}
Is it not pointless extening exception this way since all of the overriding constructors are just calling the super class Exception ?
No, it is not pointless. You can
catchthe specific exception this way and handle it specifically, rather then catching a generalException, which might not be handled in all cases.With this, you can do:
Which is not possible if you do not know how to handle a general
Exception, but you can handleMyExceptionbetter.It is also improves readability – it is better to declare a method as
throws MyException(with better name usually) thenthrows Exception– you know what can go wrong much better this way.