Navigate Exceptions topic: ) |
The ideal time to catch an error is at compile time, before you even try to run the program. However, not all errors can be detected at compile time. The rest of the problems must be handled at run time through some formality that allows the originator of the error to pass appropriate information to a recipient who will know how to handle the difficulty properly.
Improved error recovery is one of the most powerful ways that can increase the robustness of your code. Error recovery is a fundamental concern for every program you write, but it's especially important in Java, where one of the primary goals is to create program components for others to use. To create a robust system, each component must be robust. By providing a consistent error-reporting model using exceptions, Java allows components to reliably communicate problems to client code.
In Java, there are two main flows of code executions.
catch
block begins. If that is not found at the current method level the search continues at the caller method level, until a matching catch
block is found. If none is found the exception will be handled by the JVM, and usually the java program terminates.catch
"matching" block is found, that block will be executed, the exception object is passed to the block as a parameter. Then normal program execution continues after the catch
block. See Java exception handling syntax.catch
block. Exception object encapsulates the information about the error's location and its nature. All Exception objects must be inherited from the java.lang.Throwable
. See the UML diagram below.catch
keyword and specifying the exception object's class or its super-class.CustomerNotFoundException
indicate that customer was not found.Manage research, learning and skills at defaultLogic. Create an account using LinkedIn or facebook to manage and organize your Digital Marketing and Technology knowledge. defaultLogic works like a shopping cart for information -- helping you to save, discuss and share.
Visit defaultLogic's partner sites below: