Unit 4 Study Guide and Review: Exceptions
4a. Demonstrate an understanding of the roles of exceptions and exception handling in programming
- Define exception.
- Explain the interrupt model for handling exceptions.
- A program can prevent errors or detect errors when they occur at run-time. Give an example of error prevention and an example of error detection.
- What are some run-time errors that the Java run-time system can detect?
- What are some run-time errors that the Java run-time system cannot detect?
- What are some errors that a Java compiler prevents?
Read the short framing texts for sections 4, 4.1.1, and 4.1.2 of the course. Section 8.1.2 of Eck's Introduction to Programming Using Java gives examples of error situations in programming involving arrays, pointers, memory leaks, and buffers that Java detects automatically, and, thus, can be handled as exceptions. What is a memory leak? Section 8.1.3 gives examples of errors that Java does not detect automatically.
In addition to the above data and computational errors there are application errors that pertain to satisfying the requirements of the program. If the requirements are not satisfied, the program is not correct, though it may run without being interrupted or having exceptions. Requirements related errors can be addressed by using preconditions, postconditions, and test statements. For reference, review Section 8.2 of Eck's Introduction to Programming Using Java.
4b. Demonstrate an understanding of how exceptions are handled in various programming languages, including C++ and Java
- Explain the throw-try-catch model for handling exceptions. In this model, what actually is thrown? Where is 'it' thrown from? Where is it thrown to?
- What exception handling model is used by Java? Describe the throwable class hierarchy. Which subclasses have mandatory checking?
- What exception handling model is used by C++?
- Which steps of the programming process address interrupts (errors and exceptions)? What design decisions must be made for interrupts?
Java uses throws, try, and catch statements to handle exceptions. Part 1 of "Exceptions in the Java Programming Language" explained this approach. Part 2 explains the four classes of exceptions in Java. What are they? These classes include checked and unchecked exceptions. The former, checked exceptions, are those that are detected at compile time. The unchecked are detected are run time. Part 3 discusses the Error class, those exceptions that pertain to satisfying the requirements of the program. A custom exception class is defined by the programmer to handle the application errors. Section 8.3 of Eck's "Introduction to Programming Using Java" is a more thorough treatment of Java's throw-try-catch model. Does a catch block have to be in the same subroutine where the exception occurred? Understand the 'unwinding' of the call stack, which determines the order in which subroutines are searched for a catch statement for the exception.
The C++ approach to exceptions is similar to Java's and, also, uses the throws-try-catch model. Carefully examine the C++ throw-try-catch model described in "C++ Programming".
The inclusion of error and exception handling in a program is part of the design stage of the programming process. Java Error Handling outlines the design process steps for throwing and catching exceptions and gives examples of design decisions involving handling of exceptions. Any process effort involves a cost. Design and coding steps for handling exceptions trade off efficiency. In other words, a program that handles exceptions requires more space and more time to run. However, it will have higher quality with respect to correctness and robustness.
4c. Demonstrate an understanding of the differences as to how exceptions are handled in C++ and Java with regards to syntax
- Are the exception handling models used by Java and C++ different or the same? If different, what are the differences?
A careful reading of "C++ Programming" helps one deduce the following: all exceptions in C++ are unchecked, there is no 'finally' statement in C++, there is no 'throws' statement in C++, in C++ an exception can be of any type but in Java only of class throwable type, C++ has a catch all statement, namely catch (…).
Unit 4 Vocabulary
This vocabulary list includes terms that might help you with the review items above and some terms you should be familiar with to be successful in completing the final exam for the course.
Try to think of the reason why each term is included.
- Annotations
- Assertions
- Catch
- Checked exception
- Correct program
- Corrective action
- Detection
- Error
- Exception
- Finally block
- Interrupt
- Mandatory exception handling
- Memory leak
- Post condition
- Prevention
- Robust program
- Stack trace
- Throwable class
- Throws
- Try
- Throw statement
- Unchecked exception