Why do Java people frequently consume exceptions silently?

前端 未结 28 1430
傲寒
傲寒 2020-11-29 17:50

I never did any serious Java coding before, but I learned the syntax, libraries, and concepts based on my existing skills (Delphi & C#). One thing I hardly understand i

相关标签:
28条回答
  • 2020-11-29 18:17

    I disagree that rethrowing a checked exception is a better idea. Catching means handling; if you have to rethrow, you shouldn't catch. I'd add the throws clause to the method signature in that case.

    I would say that wrapping a checked exception in an unchecked one (e.g., the way Spring wraps the checked SQLException into an instance of its unchecked hierarchy) is acceptable.

    Logging can be considered handling. If the example was changed to log the stack trace using log4j instead of writing to the console, would that make it acceptable? Not much of a change, IMO.

    The real issue is what is considered exceptional and an acceptable recovery procedure. If you can't recover from the exception, the best you can do is report the failure.

    0 讨论(0)
  • 2020-11-29 18:17

    There can be many reasons why one would use catch Exception. In many cases it is a bad idea because you also catch RuntimeExceptions - and well you don't know in what state the underlying objects will be after this happens ? That is always the difficult thing with unexpected conditions: can you trust that the rest of the code will not fail afterwards.

    Your example prints the stacktrace so at least your will know what the root cause might have been. In bigger software projects it is a better idea to log these things. And lets hope that the log component does not throw exceptions either our you might end up in an infinite loop (which will probably kill your JVM).

    0 讨论(0)
  • 2020-11-29 18:18

    As pointed out, calling printStackTrace() isn't really silent handling.

    The reason for this sort of "swallowing" of the exception is that, if you keep passing the exception up the chain, you still have to handle the exception somewhere or let the application crash. So, handling it at the level it occurs with an information dump is no worse than handling it at the top level with an information dump.

    0 讨论(0)
  • 2020-11-29 18:18

    Because they haven't learned this trick yet:

    class ExceptionUtils {
        public static RuntimeException cloak(Throwable t) {
            return ExceptionUtils.<RuntimeException>castAndRethrow(t);
        }
    
        @SuppressWarnings("unchecked")
        private static <X extends Throwable> X castAndRethrow(Throwable t) throws X {
            throw (X) t;
        }
    }
    
    class Main {
        public static void main(String[] args) { // Note no "throws" declaration
            try {
                // Do stuff that can throw IOException
            } catch (IOException ex) {
                // Pretend to throw RuntimeException, but really rethrowing the IOException
                throw ExceptionUtils.cloak(ex);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题