Custom Exception class shows Unreachable catch blo

2020-04-10 03:06发布

问题:

I've created a custom Exception class that I want to use in my application:

public class MyException extends Exception {
    private static final long serialVersionUID = -2151515147355511072L;
    private String message = null;

    public MyException() {
        super();
    }

    public MyException(String message) {
        super(message);
        this.message = message;
    }

    public MyException(Throwable cause) {
        super(cause);
    }

    @Override
    public String toString() {
        return message;
    }

    @Override
    public String getMessage() {
        return message;
    }
}

But when I try to use this class, like below, it gives a compile time error.

try {
    System.out.println("this");
} catch (MyException  e) {
    // TODO: handle exception
}

Compile time error:

Unreachable catch block for MyException . This exception is never thrown from the try statement body

My question is if I'm extending Exception class & calling super in all constructors, then why this error is occurring?

回答1:

Obviously, you are not doing anything that'd generate a MyException. First write a method with the signature throws MyException, call it and then your problem is solved. Here is an example:

public void someMethod()throws MyException
{
    //some condition here.
    //if met..
    throw new MyException("cause");
}

and modify your main code as:

try {
    someMethod();
    System.out.println("this");
} catch (MyException  e) {
    // TODO: handle exception
}


回答2:

The exception you created is a checked exception and must be thrown from somewhere to catch it. Any exception created by a java developer by extending Exception class is a checked exception. And the rules applicable for checked exception will be applied on such exceptions.

Another form of exception is called Unchecked Exception and usually created by extending RuntimeException Class. A developer is free to catch such exception without an explicit need for throwing it somewhere from your code.

class Exception is also not thrown generally. I just want MyException behave like Exception.

This is what being further asked in one of the comments:

My take on this is you can think Exception class as a large container which have many different and unique(to the point) child exceptions defined. And mostly these fine grained exceptions are thrown from Java Code. In a abstraction hierarchy, Exception is at higher level (not Highest as, Throwable is sitting there). Further, as a developer we all are always interested into the finer details like what kind of Exception is thrown. However, while handling exception, we sometimes write

try{
   //some code lets assume throws IOException
    //Some code lets assume throws FileNotFoundException
 }
 catch (Exception ex) {
  //common handling which doesn't care if its IOException or FileNotFoundException
 }

You can not intervene in this exception hierarchy by just writing MyException extends Exception. By this what you are doing is your MyException is a type of Exception not itself Exception class. So, you can't replace Exception caught in catch with your MyException.



回答3:

Can you try with:

try {
    System.out.println("this");
    throw new MyException();
} catch (MyException  e) {
    // TODO: handle exception
}

Your exception wasn't thrown anywhere in the code. (try extending RuntimeException as another option)



回答4:

What the compile time error says is right "This exception is never thrown from the try statement body". You don't have anything which throws MyException