Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.

Slides:



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

Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
CS102--Object Oriented Programming
1 Chapter 17-2 Templates and Exceptions Dale/Weems.
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.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Exception Handling Handling an unexpected behaviour Unit - 08.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
 2006 Pearson Education, Inc. All rights reserved. Exception Handling in C++ CS-2303, C-Term Exception Handling in C++ CS-2303 System Programming.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 23 - Exception Handling Outline 23.1Introduction 23.2When Exception Handling Should Be Used 23.3Other.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Exception Handling: A Deeper.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
C++ Exception Handling
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
CS Advanced C++ Exception Handling Topic #5.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 13 - Exception Handling Outline 13.1Introduction 13.2When Exception Handling Should Be Used.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
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.
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.
CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 23 - Exception Handling Outline 23.1Introduction.
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.
Exception Handling Outline 23.1 Introduction
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
LECTURE LECTURE 14 Exception Handling Textbook p
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Exception Handling How to handle the runtime errors.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
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.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
C ++ MULTIPLE CHOICE QUESTION
Exception handling.
Exception Handling in C++
IS 0020 Program Design and Software Tools
Exceptions Exceptions are used to signal that an unexpected event has happened in a program C++ will generate exceptions for some errors in the program.
CS212: Object Oriented Analysis and Design
EXCEPTION HANDLING IN C++
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
Chapter 14: Exception Handling
Chapter 17 Templates and Exceptions Part 2
Exception Handling Chapter 9 Edited by JJ.
Exception Handling.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Presentation transcript:

Exception Handling

2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution procedure Syntactic error occur  Due to poor understanding of language, can solve by compiler

3 Exception Handling Exceptions are runtime errors Such as division by zero, array out of bounds, no disk space, trying to read/write data from improper address (or an unopened file)… When a run time error encountered the program terminates C++ provides built in features to detect & handle exceptions

4 Exception Handling Exceptions are 2 types Synchronous exceptions Asynchronous exceptions Synchronous  out of range index, overflow, divide by zero.. Asynchronous  beyond the control of the program such as keyboard problem…. Exception handling mechanism mainly for Synchronous errors

5 Exception Handling mechanism Exception handling mechanism detect and report an runtime error, so appropriate action can be taken Exception handling mechanism steps 1)Find an error (hit exception) 2)Inform that an error occurred (throw exception) 3)Receive the error information (catch exception) 4)Take corrective action(handle exception)

6 Exception Handling mechanism Exception handling code consists of the following segments To detect error To throw error To catch error To take appropriate action

7 Exception Handling mechanism Exception handling mechanism done with three keywords try throw catch

8 Exception Handling try block A block of statements surrounded with braces, which may generate error If suspect a statement or a set of statements may cause any runtime error, group them in a block and name it as try block try { statements; }

9 Throwing Exceptions When an error is detected in the try block it is thrown using a throw statement in the try block try { int a,b; …… if(a==0) { throw “Divide by Zero”; } cout<<“checking”; }

10 Catching Exceptions: Defined by the keyword catch Catches the exception thrown by the throw statement in the try block The catch block that catches an exception must immediately follow the try block Any thrown exception must be caught by a catch statement that has thrown the exception try { int a,b; …… if(a==0) { throw “Divide by Zero”; } cout<<“checking”; } catch (type arguments) { statments }

11 Exception Handling main() { cout << "Start\n"; try { // start a try block cout << "Inside try block\n"; throw (100); // throw an error cout << "This will not execute"; } catch (int i) { // catch an error cout << "Caught an exception -- value is: "; cout << i << "\n"; } cout << "End“; } This program displays the following output: Inside try block Caught an exception -- value is: 100 End

12 Exception Handling When try block throws an exception the program control leaves the try block and enters the catch statements After exception handling the program returns to the point after the try-catch block, not after the throw statement. Exceptions are objects used to transmit information about a problem If the type object throw matches the argument type in the catch, then catch block is executed for handling the error If they do not matches program terminates

13 Exception Handling void main() { int a,b,c; cout >a>>b; try { if(b!=0) { c=a/b;cout<<c; } else { throw(b); //throw int object } catch(int x) // catches the exception { cout<<“exception caught “ <<x; } cout<<“END”; }

14 Exception handling in function void check(int x,int y) { if(y!=0) { int z=x/y;cout<<z; } else { throw(y); } void main() { try { check(3,0); } catch(int z) { cout<<“Caught the exception”; } cout<<“END” getch(); }

15 Multiple catch Statements We can have more than one catch associated with a try. However, each catch must catch a different type of exception. For example, this program catches both integers and strings. try { if(test) throw test; else throw "Value is zero"; } catch(int i) { cout << "Caught Exception #: " << i << '\n'; } Catch(char *str) { cout << "Caught a string: "; cout << str << '\n'; }

16 Catching All Exceptions In some situation we want an exception handler to catch all exceptions Using one catch block catch(...) { // process all exceptions }

17 Catching All Exceptions try { if(test==0) throw test; // throw int if(test==1) throw 'a'; // throw char if(test==2) throw ; // throw double } catch(...) { // catch all exceptions cout << "Caught One!\n"; }

18 Rethrowing an Exception Used when an exception handler cannot process an exception Rethrow exception with the statement: t hrow Without arguments Current exception thrown to the next enclosing try/catch sequence and is caught by a catch statement listed after that enclosing try block

19 Rethrowing an Exception void show(int a,int b) { cout<<“inside function”; try { if(b==0) { throw(b); } else { cout<<a/b;} } catch(int x) { cout<<“ caught an exception”; throw; } cout<<“end of function” } void main() { try { show(10,0); } catch(int x) { cout<<“caught exception inside main”; } output inside function caught an exception caught exception inside main

20 Rethrowing an Exception When an exception is rethrown, it will not be caught by the same catch or any other catch in that group It will be caught by an outer catch statements

21 Restricting Exception We can restrict the type of exception that a function can throw To accomplish this restriction add a throw clause to the function definition Syntax data type function(parameters) throw(data type list) { function body } Here only those data types that contain in the throw will be throw by the function Attempt to call a type that not in the throw, will terminate the program

22 Restricting Exception Void function(int x) throw(int, char) { if(x==1){ throw(1);} if(x==2){throw(3.5);} if(x==3){throw(‘a’);} } Void main() { try { function(1); function(2); function(3); } catch(int) { cout<<“caught an int”; } catch(char) { cout<<“caught a char”; } catch(float) { cout<<“caught a float”; } When the function called function(2) The program will terminate Because the function restricted only to caught int and char

23 Standard Library Exception Hierarchy Base-class exception – Contains virtual function what for storing error messages Exception classes derived from exception – bad_alloc – thrown by new – bad_cast – thrown by dynamic_cast – bad_typeid – thrown by typeid – bad_exception – thrown by unexpected Instead of terminating the program or calling the function specified by set_unexpected Used only if bad_exception is in the function’s throw list

24 Standard Library Exception Hierarchy

25 bad_exception Derived from the exception class This is a special type of exception specifically designed to be listed in the dynamic-exception of a function (i.e., in its throw specifier). Throws an exception not listed in it and unexpected rethrows it, a bad_exception is automatically thrown. void myfunction () throw (int, exception::bad_exception) { throw 'x'; // throws char (not in exception-specification) }

26 bad_exception void myfunction () throw (int, exception::bad_exception) { throw 'x'; // throws char (not in exception-specification) } Void main() { try { myfunction(1); } catch(exception::bad_exception) { cout<<“caughr bad exception”; } catch(char) { cout<<“caught a char”; }

27 Exception Handling