Throwing and catching exceptions

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
CS102--Object Oriented Programming
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.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
CPSC150 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 CPSC150 Exceptions When things.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Exceptions COMPSCI 105 S Principles of Computer Science.
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Object Oriented Programming
CIS 270—Application Development II Chapter 13—Exception Handling.
Chapter 12: Exception Handling
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.
Exceptions Handling Exceptionally Sticky Problems.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
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.
HANDLING EXCEPTIONS Chapter 9. Outline  Learn about the limitations of traditional error-handling methods  Throw exceptions  Use try blocks  Catch.
Exceptions. Why exceptions? We often strive for writing portable reusable code; we are able to detect errors, however our code may be used for many different.
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.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
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.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Exception Handling in C++. Outline What exceptions are and when to use them Using try, catch and throw to detect, handle and indicate exceptions, respectively.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
Eighth Lecture Exception Handling in Java
Exception Handling in C++
Exceptions Error Handling and Recovery
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
Generics, Exceptions and Undo Command
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Handling Exceptionally Sticky Problems
More important details More fun Part 3
Java Programming Language
Why exception handling in C++?
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Chapter 14: Exception Handling
Exception Handling Chapter 9.
Chapter 12 Exception Handling and Text IO
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling Chapter 9 Edited by JJ.
Part B – Structured Exception Handling
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
Exception Handling in Java
CMSC 202 Exceptions 2nd Lecture.
CSC 143 Java Errors and Exceptions.
Handling Exceptionally Sticky Problems
Object-Oriented Programming (OOP) Lecture No. 44
Java Basics Exception Handling.
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions.
Exception Handling.
Presentation transcript:

Throwing and catching exceptions Error Handling in C++ Throwing and catching exceptions T Update for VS The only area where Java is superior Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Exceptions Exception: An unexpected problem that prevents normal continuation of the algorithm Examples: divide by zero using a NULL pointer It could be a program specific error as well Copyright © 1999-2014 - Curt Hill

Traditional Exception Handling In many languages there is no error handling If we think there is a good chance that a divisor could be zero, we could test it: if(k==0) { // handle error … else j = m/k; Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Problems There are just too many possible ways to generate an exception If we tested each one with an if most of the code would be error testing We set a trap instead that covers more of the program Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Another Possibility There exists an include: <assert.h> This contains a function: assert(int) This is used for testing assertions If the int evaluates to a true the assert does nothing If false it aborts the program with an error message Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Example Consider the following code: int array[MAX]; … index = …; assert(index>=0 && index<MAX); array[index] = …; This will abort the program if the index is not in the correct range Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Assert Issues Asserts are typically used to test assumptions Their being false generally indicates a logic error earlier in the program Bad computations Lack of testing of input Favorite way to abort a program: assert(false); There is also no recovery Makes it undesirable for error checking of an expected nature Copyright © 1999-2014 - Curt Hill

C++ Exception Handling There is a better way to handle errors that are not logic errors C++ exception handling is having one error handling routine that intercepts all divide by zero exceptions At least all that are in the same call chain The exception handler can be a long ways from the exception Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Two Parts Finding the error and announcing it as an error Throwing an exception This uses the throw keyword Handling the exception Recovery or error handling This uses the try and catch keywords Copyright © 1999-2014 - Curt Hill

Catching the exception Uses the try catch block try introduces a compound statement This is followed by one or more catch clauses Each catch clause is like a one parameter method that handles that kind of exception Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill try catch syntax Form is: try { // code . . . } catch (ExceptionType_1 e) { handle exception 1} catch (ExceptionType_2 e) { handle exception 2 } ... catch (ExceptionType_N e) { handle exception N } Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Form of catch catch (type parm){stmts} Parameter is any type The catch matches the type of the exception Limited scope, only usable in the compound statement Compound statement is like a method body and the Exception (or subclass) is the only parameter Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Catch Parameter Needs to match the throw parameter Carries information from the site of the error to the catch clause Any type may be thrown and later caught System errors are usually char * The error type of an elipsis will catch anything: catch(…) { } but gives no information Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Example try catch Catch conversion error: try { d = str.ToDouble(); } catch(wxString e){ MessageBoxW (NULL,e, L”oops”,MB_OK); } The error can be in the try or in any function called from within the try Copyright © 1999-2014 - Curt Hill

Exceptions and the throw Many exceptions are thrown automatically These require no intervention on our part They typically throw a C style string, which is the message Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Throwing an Exception Use reserved word throw and the new type Example: if(j==0) throw “j was zero\n”; User defined throws may place a suffix on the function: int func(…) throw (char *) { indicates the exceptions that could be thrown Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Cause and Effect The throw and catch do not have to be near each other The throw is like a super return The current function is exited as well as any between this one and the function containing the try and catch Consider in next screen, a main function with a try catch that calls Fun_1, which calls Fun_2, which calls Oops, which throws an exception Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Call example throw Oops Oops(...); Fun_2 Fun_2(…) Fun_1 try{ Fun_1(…) catch(…) main Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Exception Processing The call stack between throw and catch is flushed Every call, every parameter and every local is destroyed The function and all those in between are terminated They may be re-executed later, but they can not be resumed Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Nested Trys We do not usually nest a try within another However, to get to our exception we may go through several try catch pairs in several methods The exception is always caught by the closest try Closest in terms of most recently called function Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Second call example throw Oops Oops(...); Fun_2 try { Fun_2(…); catch(…){…} Fun_1 try{ Fun_1(…) catch(…){…} main Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Multiple Catches A try catch statement can have many catch clauses They are tried in order from top to bottom What the clause handles is based on the type of the parameter to the catch The first one that matches handles the error Copyright © 1999-2014 - Curt Hill

The Catch Parameter Type Two catch parameters of different types usually cannot interfere with each other Thus the order of: catch (int ie) {…} catch (char * ce) {…} does not matter This is not true if the parameters are in the same inheritance hierarchy Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Recall Inheritance maintains an “is a” relationship If a person is a class and student is derived from that person class, then student is a person Thus inheritance can influence the order of catch clauses following a try block Copyright © 1999-2014 - Curt Hill

Inheritance and Catch Parameters Suppose: class Exc {…}; class Exc2:public Exc {…}; Exc2 has an isa relationship with Exc Thus the order of: catch (Exc e) {…} catch (Exc2 ce) {…} does matter The first one catches a thrown Exc and the second catches Exc2 Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Again Suppose: class Exc {…}; class Exc2:public Exc {…}; Thus the order of: catch (Exc ce) {…} catch (Exc2 e) {…} does matter Since Exc2 isa Exc the first catch will catch both Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Right and Wrong Exc2 is a descendant of Exc Thus the order of: catch (Exc2 ce) {…} catch (Exc e) {…} both catches may catch something Thus the order of: catch (Exc ce) {…} catch (Exc2 e) {…} only the first may catch something Copyright © 1999-2014 - Curt Hill

Multiple trys and catches There can be as many try catch statements as wanted The smaller the area protected, the more specific the exception handling can be The larger the area the more coverage the exception handling Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Throw and Throw Again A catch may end up with an exception that it does not know how to handle It merely issues a throw and lets someone else handle it It better be nested in an if so that all exceptions are not thrown Copyright © 1999-2014 - Curt Hill

User Defined Exceptions A truly systematic programmer will define their own exception classes These will be thrown instead of simple things like int or char * Some libraries, such as VCL, already have an exception class that may be sub-classed These are often used for application specific problems Copyright © 1999-2014 - Curt Hill

Classes and Exceptions A try catch only protects a single call chain There is no standard means for protecting every method of a class This includes the Form class as well Each public method needs it’s own try catch for full protection Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill Differing Systems Unlike other languages, such as Java, the libraries are not standard in C++ Each implementation can have their own system of error objects Embarcadero’s CBuilder and Microsoft’s Visual Studio both have an elaborate class system Dev-C++ does not Copyright © 1999-2014 - Curt Hill

Copyright © 1999-2014 - Curt Hill New Things Keywords try catch throw Class Exception Copyright © 1999-2014 - Curt Hill