Custom Exception class shows Unreachable catch block everytime

后端 未结 4 617
無奈伤痛
無奈伤痛 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: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
    }
    

提交回复
热议问题