Exception is never thrown in body of corresponding try statement

后端 未结 4 1763
别那么骄傲
别那么骄傲 2020-12-03 05:14

I have a problem with exception handling in Java, here\'s my code. I got compiler error when I try to run this line: throw new MojException(\"Bledne dane\");. T

4条回答
  •  忘掉有多难
    2020-12-03 05:43

    As pointed out in the comments, you cannot catch an exception that's not thrown by the code within your try block. Try changing your code to:

    try{
        Integer.parseInt(args[i-1]); // this only throws a NumberFormatException
    }
    catch(NumberFormatException e){
        throw new MojException("Bledne dane");
    }
    

    Always check the documentation to see what exceptions are thrown by each method. You may also wish to read up on the subject of checked vs unchecked exceptions before that causes you any confusion in the future.

提交回复
热议问题