Exception is swallowed by finally

前端 未结 7 1884
攒了一身酷
攒了一身酷 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:19

    This is because you issue a return statement before the exception is passed trough and thus a valid value is returned. You cannot both return a value and throw an exception.

    Removing the finally block around the return will give the behaviour you want.

    0 讨论(0)
  • 2020-12-09 00:28

    It will return 2 because

    finally always execute

    0 讨论(0)
  • 2020-12-09 00:30

    The IOException class is not a child of the ArrayIndexOutOfBoundsException class so the catch part will be never executed.

    If you change to it this it will return 3.

    static int retIntExc() throws Exception{
            int result = 1;
            try {
                result = 2;
                throw new ArrayIndexOutOfBoundsException ("Exception rised.");
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println(e.getMessage());
                result = 3;
            } finally {
                return result;
            }
        }
    
    0 讨论(0)
  • 2020-12-09 00:30

    By putting return in finally method you override thrown exception and result is returned instead. Your code should be something like this:

    static int retIntExc() throws Exception{
            int result = 1;
            try {
                result = 2;
                throw new IOException("Exception rised.");
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println(e.getMessage());
                result = 3;
            } finally {
                // do something
            }
            // it gets here only when exception is not thrown
            return result;
        }
    
    0 讨论(0)
  • 2020-12-09 00:33

    I don't see why this wouldn't be expected behaviour. By the end of this code block result is equal to 2.

    static int retIntExc() throws Exception{
            int result = 1;
            try {
                result = 2;
    

    You then throw an exception, but your catch block catches exceptions of a different type so nothing is executed.

            throw new IOException("Exception rised.");
        } catch (ArrayIndexOutOfBoundsException e) {
              ...
        }
    

    The finally block is guaranteed to be executed, so the final step is to return 2.

    This successful return overrules the bubbling exception. If you want the exception to continue to bubble then you must not return in the finally block.

    0 讨论(0)
  • 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.

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