Download presentation
Presentation is loading. Please wait.
Published byRuth Merritt Modified over 9 years ago
1
1 Chapter Eight Exception Handling
2
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
3
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
4
4 Objectives How to throw an Exception How to trace Exceptions through the call stack How to create your own Exception classes
5
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
6
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
7
7 Purposely Generating a SystemException You can deliberately generate a SystemException exception by forcing a program to contain an error
8
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
9
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
10
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
11
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
12
12 Understanding Object-Oriented Exception- Handling Methods General form of a try…catch pair
13
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
14
14 Using the Exception Class’s ToString() Method and Message Field Output of UsingTheException program when user enters 0 for second number
15
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
16
16 Catching Multiple Exceptions TwoErrors class with two catch blocks
17
17 Catching Multiple Exceptions Output of TwoErrors program
18
18 Catching Multiple Exceptions If you reverse the two try statements within the TwoErrors class, the output of the program changes
19
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
20
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
21
21 Catching Multiple Exceptions Program with unreachable catch block
22
22 Catching Multiple Exceptions Error message generated by UnreachableCatch program
23
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
24
24 Using the finally Block General form of a try…catch block with a finally block
25
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
26
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)
27
27 Throwing Exceptions Two executions of TrySoccerPlayer1 program
28
28 Throwing Exceptions Execution of TrySoccerPlayer2 program
29
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
30
30 Tracing Exceptions Through the Call Stack The StackTrace field can be a useful debugging tool
31
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
32
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
33
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
34
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.