Kurt Nørmark ©
Department of Computer Science, Aalborg University, Denmark
Abstract Previous lecture Next lecture Index References Contents | In object-oriented programming languages errors can be represented as objects. Run-time errors are referred to as exceptions. In this section we will study how exceptions can be represented as objects, and how errors can be classified by means class hierarchies. In addition, we will take a look at the facilities in C# that allows us to throw and handle the exceptions. |
Fundamental Questions about Exception Handling |
What is the motivation? Slide Annotated slide Contents Index References Textbook |
|
|
What is an error? Slide Annotated slide Contents Index References Textbook |
|
|
|
What is normal? What is exceptional? Slide Annotated slide Contents Index References Textbook |
|
|
Examples of normal and exceptional aspects Slide Annotated slide Contents Index References |
|
Program: A recursive Factorial function. |
|
|
Program: An iterative Factorial function. |
|
Program: An iterative Factorial function with a precondition - not C# 3.0. |
|
When are errors detected? Slide Annotated slide Contents Index References Textbook |
|
|
|
How are errors handled? Slide Annotated slide Contents Index References Textbook |
|
|
Where are errors handled? Slide Annotated slide Contents Index References Textbook |
|
|
Conventional Exception Handling |
Exception Handling Approaches Slide Annotated slide Contents Index References Textbook |
|
|
|
Mixing normal and exceptional cases Slide Annotated slide Contents Index References Textbook |
|
Program: A file copy program. |
|
Program: A file copy program with excessive error handling. |
|
|
Object-oriented Exception Handling |
Errors as Objects Slide Annotated slide Contents Index References Textbook |
|
|
Classification of Errors Slide Annotated slide Contents Index References Textbook |
|
A sample error classification hierarchy |
Exceptions and Exception Handling in C# |
Exceptions in a C# program Slide Annotated slide Contents Index References Textbook |
|
Program: A C# program with an exception. |
|
Program: Output from the C# program with an exception. |
|
|
The try-catch statement C# Slide Annotated slide Contents Index References Textbook |
|
Syntax: The syntax of the try-catch statement C# |
|
|
Handling exceptions in C# Slide Annotated slide Contents Index References Textbook |
|
Program: A C# program with a handled exception. |
|
Program: Output from the C# program with a handled exception. |
|
Exercise 9.2. Exceptions in Convert.ToDouble | The static methods in the static class System.Convert are able to convert values of one type to values of another type. Consult the documentation of System.Convert.ToDouble. There are several overloads of this method. Which exceptions can occur by converting a string to a double? Write a program which triggers these exceptions. Finally, supply handlers of the exceptions. The handlers should report the problem on standard output, rethrow the exception, and then continue. |
|
The hierarchy of exceptions in C# Slide Annotated slide Contents Index References Textbook |
|
|
|
Exercise 9.4. Exceptions in class Stack | In the lecture about inheritance we specialized the abstract class Stack. Now introduce exception handling in your non-abstract specialization of Stack. I suggest that you refine your own solution to the previous exercise. It is also possible to refine my solution. More specifically, introduce one or more stack-related exception classes. The slide page "Raising and throwing exceptions in C#" tells you how to do it. Make sure to specialize the appropriate pre-existing exception class! Arrange that Push on a full stack and that Pop/Top on an empty stack throw one of the new exceptions. Also, in the abstract stack class, make sure that ToggleTop throws an exception if the stack is empty, or if the stack only contains a single element. Finally, in a sample client program such as this one, handle the exceptions that are thrown. In this exercises it is sufficient to report the errors on standard output. |
Exercise 9.4. More exceptions in class Stack | In continuation of the previous exercise, we now wish to introduce the following somewhat unconventional handling of stack exceptions:
With these ideas, most stack programs will be able to terminate normally (run to the end). I suggest that you introduce yet another specialization of the stack class, which specializes Push, Pop, and Top. The specialized stack operations should handle relevant stack-related exceptions, and delegate the real work to its superclass. Thus, in the specialized stack class, each stack operation, such as Push, you should embed base.push(el) in a try-catch control structure, which repairs the stack - as suggested above - in the catch clause. |
The class System.Exception in C# Slide Annotated slide Contents Index References Textbook |
|
|
|
Handling more than one type of exception in C# Slide Annotated slide Contents Index References Textbook |
|
Program: A C# program with an exception handling attempt - not a success. |
|
Program: Output from the C# program with an unhandled exception. |
|
Program: A C# program with an exception handling attempt - now successful. |
|
Program: Output from the C# program with a handled exception. |
|
|
Propagation of exceptions in C# Slide Annotated slide Contents Index References Textbook |
|
Program: A C# program with simple propagation of exception handling. |
|
Program: Output from the C# program simple propagation. |
|
Program: A C# program with deeper exception propagation chain. |
|
Program: Output from the C# program deeper exception propagation. |
|
Exercise 9.5. Revealing the propagation of exceptions | We have written a program that reveals how exceptions are propagated. In the program output, we see that the calling chain is Main, M, N, P. The program output does not, however, reveal that the chain is followed in reverse order in an attempt to find an appropriate exception handler. Revise the program with handlers in M, N, and P that touch the exception without actually handling it. The handlers should reveal, on standard output, that P, N, and M are passed in an attempt to locate a relevant exception handler. Rethrow the exception in each case. See here how this can be done. |
Raising and throwing exceptions in C# Slide Annotated slide Contents Index References Textbook |
|
Syntax: The syntax of exception throwing in C# |
|
Program: A throw statement in C#. |
|
Program: Definition of the exception class. |
|
Try-catch with a finally clause Slide Annotated slide Contents Index References Textbook |
|
Syntax: The syntax of the try-catch-finally statement C# |
|
|
Program: Illustration of try-catch-finally. |
|
Program: Output from the try-catch-finally program. |
|
Rethrowing an exception Slide Annotated slide Contents Index References Textbook |
|
|
Program: Rethrowing an exception. |
|
Program: Output from the program that rethrows an exception. |
|
|
Raising an exception in an exception handler Slide Annotated slide Contents Index References Textbook |
|
|
Program: Raising and throwing a new exception. |
|
Program: Output from the program that raises a new exception. |
|
Program: Raising and throwing a new exception, propagating original inner exception. |
|
Program: Output from the program that raises a new exception, with inner exception. |
|
Comparison with exception handling in Java Slide Annotated slide Contents Index References |
|
|
Program: A Java program - does not compile.
Main calls explosion without handling or specifying Problem. |
|
Program: A Java program - main handles Problem. |
|
Program: A Java program - main specifies Problem. |
|
Recommendations about exception handling |
Recommendations about exception handling Slide Annotated slide Contents Index References Textbook |
|
|
Recommendations about exception handling Slide Annotated slide Contents Index References Textbook |
|
|
Chapter 9: Exception Handling
Course home Author home About producing this web Previous lecture (top) Next lecture (top) Previous lecture (bund) Next lecture (bund)
Generated: February 7, 2011, 12:18:34