An exceptional condition is a problem that prevents the continuation of the current method or scope. It’s important to distinguish an exceptional condition from a normal problem, when you have enough information in the current context to somehow cope with the difficulty.
When we throw an exception, two things happen.
- The exception object is created in the same way as any Java object: on the heap, with new.
- The current path of execution (the one you can't continue) is stopped and the reference for the exception object is ejected from the current context. Now the exception-handling mechanism takes over and begins to look for an appropriate place(the exception handler) to continue executing the program.
The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause. For the purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of eitherRuntimeExceptionorErrorare regarded as checked exceptions.
Instances of two subclasses, Error and Exception, are conventionally used to indicate that exceptional situations have occurred. Typically, these instances are freshly created in the context of the exceptional situation so as to include relevant information (such as stack trace data).
The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.
There are two basic models in exception-handling theory: termination (Java support this) and resumption.
references:
1. On Java 8 - Bruce Eckel
2. https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html
本文深入探讨了Java中异常处理的基本原理,包括异常条件、异常对象的创建与抛出过程,以及异常处理机制如何寻找合适的处理器继续执行程序。同时,区分了运行时异常和检查异常,并介绍了Throwable、Error及Exception类在异常处理中的角色。
1万+

被折叠的 条评论
为什么被折叠?



