Handling Anomalies: Errors and Exceptions, Page 5
You can catch exceptions at various levels of granularity. You can catch all exceptions or just check for specific exceptions, such as arithmetic exceptions. If your code does not catch an exception, the Java runtime will—and it won't be happy about it!
In this example, as you have already seen, the division by zero within the try block will cause an arithmetic exception. If an exception is actually generated (thrown) outside a try block, the program would most likely have been terminated. However, because the exception was thrown within a try block, the catch block is checked to see whether the specific exception (in this case, an arithmetic exception) was planned for.
Diagram 5: Generating an Error—Catching the exception.
As seen in Diagram 5, the operating system and the application are, in effect, working together to handle the problems. Although the operating system (or Virtual Machine) does actually generate the exceptions, the application detects them and can handle them as desired. Figure 4 shows what the user sees from the various messages printed after the exception is handled.
Figure 4: Generating an Error—Catching the exception.
It is almost certain that every system will encounter unforeseen problems. Thus, it is not a good idea to simply ignore potential errors. The developer of a good class (or any code, for that matter) anticipates potential errors and includes code to handle these conditions when they are encountered.
The rule of thumb is that the application should never crash. When an error is encountered, the system should either fix itself and continue, or exit gracefully without losing any data that's important to the user.
It's a good idea to use a combination of the methods described here to make your program as bulletproof for your user as possible.
- Java Primer Plus: Supercharging Web Applications With the Java Programming Language. Tyma, Torol, Downing. ISBN: 157169062X
- Object-oriented design in Java. Stephen Gilbert; Bill McCarty. ISBN: 1571691340
About the Author
Matt Weisfeld is a faculty member at Cuyahoga Community College (Tri-C) in Cleveland, Ohio. Matt is a member of the Information Technology department, teaching programming languages such as C++, Java, C#, and .NET as well as various web technologies. Prior to joining Tri-C, Matt spent 20 years in the information technology industry, gaining experience in software development, project management, business development, corporate training, and part-time teaching. Matt holds an MS in computer science and an MBA in project management. Besides The Object-Oriented Thought Process, which is now in its second edition, Matt has published two other computer books, and more than a dozen articles in magazines and journals such as Dr. Dobb's Journal, The C/C++ Users Journal, Software Development Magazine, Java Report, and the international journal Project Management. Matt has presented at conferences throughout the United States and Canada.
Page 5 of 5