CS102: Introduction to Computer Science II
Unit 4: Exceptions
A programmer must ensure the safe execution of his or her code. In other words, if an error occurs, the program should present the user with relevant information and then quit gracefully. The built-in C++ structures used to accomplish this goal are known as "Exceptions." This unit will introduce you to the concept of throwing and catching Exceptions when something goes wrong with your software, explain how they are used in both C++ and Java, and teach you how to handle them. By the end of this unit, you will be able to write safe programs designed to perform gracefully when/if errors occur.
Completing this unit should take you approximately 4 hours.
Upon successful completion of this unit, you will be able to:
- demonstrate an understanding of the roles of exceptions and exception handling in programming;
- demonstrate an understanding of how exceptions are handled in C++ and Java; and
- apply and compare exception handling in C++ and Java programs.
An exception is a runtime error that interrupts the execution of a program statement. When an exception occurs, the runtime system transfers control to either a runtime routine that handles the exception by printing a message and then terminating the program or to a programmer-provided routine that handles the exception by executing instructions that take corrective action. This video gives a Java example of an exception handler using 'throw' and 'catch' blocks.
Read section 8.1.
An error can be a syntax error, a runtime error, or a logic error. The language compiler or interpreter can detect syntax errors. A runtime error is an unexpected event involving incorrect semantics and interrupts the execution of an instruction. A logic error results in program behavior that does not satisfy the requirements, e.g. an incorrect solution. The resource reading describes some semantic errors that Java can prevent and some that Java can not detect. In the latter case, the Java developer should build program features that either avoid or detect those errors.
Read section 8.2 for an explanation of how to prevent errors by writing correct programs and writing program statements to take corrective action or to prevent errors.
Runtime errors and incorrect results when executing a program are addressed by detecting them and taking corrective action when they occur, or by preventing them from occurring in the first place. Corrective action and prevention depend on an understanding of the programming process and the identification of the root cause of errors. The programming process consists of several stages: specifying the requirements for the program, designing the program, implementing the program using a programming language, testing the program, and deploying the program for operation by users.
Errors should be detected as early as possible in the programming process, and each process stage should have a verification activity: requirements, design, code, and even tests should be verified for correctness. Each stage should also be validated (that is, checked that they satisfy the requirements) to prevent errors.
This article article illustrates the Java flow to handle an exception. Java uses 'try' and 'catch' blocks for the code that handles the exception, and a 'throw' keyword used in the method where the exception occurs. There are 4 sections to the reading, each after the first accessed by clicking <next>. Be sure to read each.
There are several design decisions and are addressed in the design activity of the programming process, including what exceptions to handle, where to place the exceptions handlers, the correspondence of try's to 'catches', the nesting of exception handlers, and which methods will have the throw keywords.
Read section 8.3, which is a continuation of the previous sections on correctness and robustness, and elaborates on exceptions in Java.
Read this overview of the role of exceptions generated by the C++ library. This section also covers throw and try/catch concepts.