1 chapter eight exception handling. 2 objectives learn about exceptions and the exception class how...

34
1 Chapter Eight Exception Handling

Upload: ruth-merritt

Post on 11-Jan-2016

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

1

Chapter Eight

Exception Handling

Page 2: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

2

Objectives

• Learn about exceptions and the Exception class

• How to purposely generate a SystemException

• Learn about traditional error-handling methods

• Learn about object-oriented exception-handling methods

Page 3: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

3

Objectives

• How to use the Exception class’s ToString() method and Message field

• How to catch multiple Exceptions • How to use the finally block• How to handle an Exception with a loop

Page 4: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

4

Objectives

• How to throw an Exception• How to trace Exceptions through the call stack• How to create your own Exception classes

Page 5: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

5

Understanding Exceptions

• An exception is any error condition or unexpected behavior in an executing program

• Certain errors are called exceptions because they are not usual occurrences

• The object-oriented technique used to manage exceptions make up the group of methods known as exception handling

• In C#, all exceptions are objects

Page 6: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

6

Understanding Exceptions

• Most exceptions you will use derive from two classes that in turn derive from the Exception class– The predefined Common Language Runtime

exception classes derived from SystemException– The user-defined application exception classes you

derive from ApplicationException

Page 7: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

7

Purposely Generating a SystemException

• You can deliberately generate a SystemException exception by forcing a program to contain an error

Page 8: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

8

Purposely Generating a SystemException

• The DivideByZeroException object below was generated automatically by C#

• Just because an Exception occurs when an Exception object is created, you don’t necessarily have to deal with it

Page 9: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

9

Understanding Object-Oriented Exception-Handling Methods

• In object-oriented terminology, you “try” a procedure that may not complete correctly

• A method that detects an error condition or Exception “throws” an Exception

• The block of code that processes the error “catches” the Exception

Page 10: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

10

Understanding Object-Oriented Exception-Handling Methods

• When you write a block of code in which something can go wrong, you can place the code in a try block, consisting of:– The keyword try– An opening curly brace– Statements that might cause Exceptions– A closing curly brace

Page 11: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

11

Understanding Object-Oriented Exception-Handling Methods

• You create a catch block with the following elements:– The keyword catch, followed by an opening parenthesis, the

Exception type, a name for an instance of the Exception type, and a closing parenthesis

– An opening curly brace– Statements that take the action you want to use to deal with the

error condition– A closing curly brace

Page 12: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

12

Understanding Object-Oriented Exception-Handling Methods

• General form of a try…catch pair

Page 13: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

13

Using the Exception Class’s ToString() Method and Message Field

• Any Exception generated from within a try block would be caught by a catch block whose argument is an Exception type (e.g., Exception e); there is no way to confirm the origin of the Exception

• You can use the ToString() method to provide a descriptive error message

• The Exception class also contains a field named Message that contains useful information about an Exception

Page 14: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

14

Using the Exception Class’s ToString() Method and Message Field

• Output of UsingTheException program when user enters 0 for second number

Page 15: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

15

Catching Multiple Exceptions

• You can place as many statements as you need within a try block, and you can catch as many different Exceptions as you want

• If you place more than one statement in a try block, only the first error-generating statement throws an Exception

• When multiple catch blocks are present, they are examined in sequence until a match is found for the Exception that occurred

Page 16: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

16

Catching Multiple Exceptions

• TwoErrors class with two catch blocks

Page 17: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

17

Catching Multiple Exceptions

• Output of TwoErrors program

Page 18: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

18

Catching Multiple Exceptions

• If you reverse the two try statements within the TwoErrors class, the output of the program changes

Page 19: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

19

Catching Multiple Exceptions

• When you want to execute the same code, no matter what type of Exception occurs, you can use only one catch block, which receives type Exception

Page 20: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

20

Catching Multiple Exceptions

• The Exception class is the base class for all Exception objects and therefore can reference all Exception descendants

• The catch block in the previous code accepts all Exception argument types

• When you list multiple catch blocks following a try, you must be careful that some catch blocks don’t become unreachable

Page 21: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

21

Catching Multiple Exceptions

• Program with unreachable catch block

Page 22: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

22

Catching Multiple Exceptions

• Error message generated by UnreachableCatch program

Page 23: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

23

Using the finally Block

• The code within a finally block executes whether or not the try block identifies an Exception

• Typically, the finally block is used to perform clean-up tasks

• When you include a finally block, you are assured that the finally statements will execute before the program is abandoned

Page 24: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

24

Using the finally Block

• General form of a try…catch block with a finally block

Page 25: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

25

Handling an Exception with a Loop

• Different programs require different ways of handling Exceptions

• In some cases, the try-catch sequence could be placed in a loop that continues to execute until the code is successful

Page 26: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

26

Throwing Exceptions

• An advantage of using object-oriented exception-handling techniques is the ability to deal with Exceptions appropriately as you make conscious decisions about how to handle them

• When methods from other classes throw Exceptions, they don’t have to catch them

• When you design your own classes that might cause Exceptions, you should create them to throw the Exception but not to handle it

• Handling an Exception should be left to the client (the program that uses the class)

Page 27: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

27

Throwing Exceptions

• Two executions of TrySoccerPlayer1 program

Page 28: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

28

Throwing Exceptions

• Execution of TrySoccerPlayer2 program

Page 29: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

29

Tracing Exceptions Through the Call Stack

• The memory location where the computer stores the list of locations to which the system must return (after method calls) is known as the call stack

• When a method throws an Exception, if the method does not catch it, then the Exception is thrown to the next method “up” the call stack

• You can print the value of the StackTrace field to display a list of methods in the call stack so you can determine the location of the Exception

Page 30: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

30

Tracing Exceptions Through the Call Stack

• The StackTrace field can be a useful debugging tool

Page 31: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

31

Creating Your Own Exception Classes

• You can create your own customized Exception class for your application

• To create your own Exception that you can throw, you should extend the ApplicationException class

• You should not create an excessive number of special Exception types for your class because it adds a level of complexity to your program

Page 32: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

32

Chapter Summary

• An exception is any error condition or unexpected behavior in an executing program

• You can purposely generate a SystemException exception by forcing a program to contain an error

• When you think an error will occur frequently, it is most efficient to handle it in the traditional way, with if statements

• In object-oriented terminology, you “try” a procedure that may not complete correctly

Page 33: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

33

Chapter Summary

• Every Exception object contains a ToString() method and a Message field

• You can place as many statements as you need within a try block, and you can catch as many different Exceptions as you want

• When you have actions to perform at the end of a try…catch sequence, you can use a finally block

• When you want to keep trying a block of code until some value or state within a program is correct, you can place a try…catch block within a loop

Page 34: 1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about

34

Chapter Summary

• When methods throw Exceptions, they don’t have to catch them; instead, the program that calls a method that throws an Exception can catch it and determine what to do

• When a method throws an Exception, if the method does not catch it, then the Exception is thrown to the method that called the offending method

• To create your own Exception that you can throw, you should extend the ApplicationException class