Custom Exception class shows Unreachable catch block everytime

后端 未结 4 615
無奈伤痛
無奈伤痛 2021-01-21 11:59

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

public class MyException extends Exception {
    private static final long serialVer         


        
相关标签:
4条回答
  • 2021-01-21 12:37

    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.

    0 讨论(0)
  • 2021-01-21 12:41

    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
    }
    
    0 讨论(0)
  • 2021-01-21 12:41

    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)

    0 讨论(0)
  • 2021-01-21 12:50

    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

    0 讨论(0)
提交回复
热议问题