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

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
1 Handling Exceptions COSC 1567 C++ Programming Lecture 11.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within.
C# Programming: From Problem Analysis to Program Design1 Debugging and Handling Exceptions C# Programming: From Problem Analysis to Program Design 3 rd.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
Exceptions. 2 Objectives Introduce C# exception handling –library exception types –custom exceptions Describe keywords used for exception handling –try.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Dr. Abraham. Exception Any problem that VB or OS could not handle Robust program A program that performs well not only under ordinary conditions but also.
Object Oriented Programming
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
Chapter 12: Exception Handling
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
VB.Net - Exceptions Copyright © Martin Schray
Introduction to Exception Handling and Defensive Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
HANDLING EXCEPTIONS Chapter 9. Outline  Learn about the limitations of traditional error-handling methods  Throw exceptions  Use try blocks  Catch.
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
Exceptions, handling exceptions & message boxes Year 11 Information Technology.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 14 Event-Driven Programming with Graphical User Interfaces.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
C# Exceptions 1 CNS 3260 C#.NET Software Development.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
16 Exception Handling.
Java Programming Fifth Edition
Chapter 14: Exception Handling
Exception Handling Chapter 9 Edited by JJ.
Part B – Structured Exception Handling
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Object-Oriented Programming Using C++ Second Edition
Chapter 11: Exception Handling
Presentation transcript:

1 Chapter Eight Exception Handling

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 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 Objectives How to throw an Exception How to trace Exceptions through the call stack How to create your own Exception classes

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 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 Purposely Generating a SystemException You can deliberately generate a SystemException exception by forcing a program to contain an error

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 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 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 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 Understanding Object-Oriented Exception- Handling Methods General form of a try…catch pair

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 Using the Exception Class’s ToString() Method and Message Field Output of UsingTheException program when user enters 0 for second number

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 Catching Multiple Exceptions TwoErrors class with two catch blocks

17 Catching Multiple Exceptions Output of TwoErrors program

18 Catching Multiple Exceptions If you reverse the two try statements within the TwoErrors class, the output of the program changes

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 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 Catching Multiple Exceptions Program with unreachable catch block

22 Catching Multiple Exceptions Error message generated by UnreachableCatch program

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 Using the finally Block General form of a try…catch block with a finally block

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 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 Throwing Exceptions Two executions of TrySoccerPlayer1 program

28 Throwing Exceptions Execution of TrySoccerPlayer2 program

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 Tracing Exceptions Through the Call Stack The StackTrace field can be a useful debugging tool

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 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 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 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