Exception is swallowed by finally

前端 未结 7 1916
攒了一身酷
攒了一身酷 2020-12-08 23:54
static int retIntExc() throws Exception{
    int result = 1;
    try {
        result = 2;
        throw new IOException(\"Exception rised.\");
    } catch (ArrayInd         


        
7条回答
  •  不知归路
    2020-12-09 00:38

    The finally block executes no matter what exception is thrown. It doesn't just execute after the exceptions are caught by the catch blocks you declare. It executes after the try block and exceptions caught if any. If your method throws an exception, it can't return anything unless you swallow it within your method and return result. But you can't have both.

    Also, unless your method has any other code, ArrayIndexOutOfBoundsException will never be encountered either.

提交回复
热议问题