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
1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Exception Handling Introduction Try Throw Catch. Exception Handling Use exception handling to write programs that are –Clearer –More robust –More fault-tolerant.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
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
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
EXCEPTION HANDLING ► COURSE : MSc Computers and Network engineering. ► Name : Kirthidhar Gandham ► Module : Object Oriented Methods ( ) ► Module.
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.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
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.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
計算機概論實習 A Simple Way to Handle Error if(b != 0) { cout
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
Exceptions COMPSCI 105 S Principles of Computer Science.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Object Oriented Programming
Handling ErrorstMyn1 Handling Errors Up to this point we haven't worried much about errors or exceptions. First, let's distinguish between errors and exceptions.
Chapter 12: Exception Handling
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.
1 CSC241: Object Oriented Programming Lecture No 27.
Slides Credit Umair Javed LUMS Web Application Development.
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 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.
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.
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.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
LECTURE LECTURE 14 Exception Handling Textbook p
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception Handling How to handle the runtime errors.
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.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Exception handling.
EXCEPTION HANDLING IN C++
Java Programming Fifth Edition
Andy Wang Object Oriented Programming in C++ COP 3330
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 17 Templates and Exceptions Part 2
Chapter 12 Exception Handling
Exception Handling.
Object-Oriented Programming (OOP) Lecture No. 43
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 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 Exception handling code consists of the following segments To detect error To throw error To catch error To take appropriate action

7 Exception Handling Exception handling mechanism done with three keywords try throw catch try block A block of statements surrounded with braces, which may generate error If you suspect a statement or a set of statements may cause any runtime error, group them in a block and name it as try

8 Exception Handling try { statements; } throw 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”; }

9 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: Start Inside try block Caught an exception -- value is: 100 End

10 Exception Handling catch 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 When try block throws an exception the program control leaves the try block and enteres the catch statements 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

12 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”; }

13 Exception Handling 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(); }

14 Exception Handling

15 Exception Handling Using Multiple catch Statements As stated, you can have more than one catch associated with a try. In fact, it is common to do so. 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(const char *str) { cout << "Caught a string: "; cout << str << '\n'; } Each catch statement responds only to its own type.

16 Exception Handling Catching All Exceptions In some circumstances you will want an exception handler to catch all exceptions This is easy to accomplish. Simply use this form of catch. catch(...) { // process all exceptions }

17 Exception Handling 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"; }