計算機概論實習 2007-06-01. 2 A Simple Way to Handle Error if(b != 0) { cout << "a/b = " << double(a / b) << endl; } else { cout << "b can not be zero" << endl;

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
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.
1 Chapter 17-2 Templates and Exceptions Dale/Weems.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
Exception Handling Handling an unexpected behaviour Unit - 08.
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.
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.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.
CS Advanced C++ Exception Handling Topic #5.
計算機概論實習 Review of Practice 8 (P8) PLEASE write a program that when you fail to open a file, you have to throw a exception. Directly using.
計算機概論實習 What is Class In C++, class is the keyword which means encapsulation. Property and method are used to define what is class. Materialize.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
計算機概論實習 Claim a Parameter int a; a = 5; typenameaddress inta0x0001 addressvalue 0x0001 0x0002 0x0003 0x0004 0x0005 0x0006 0x0007 0x0008.
計算機概論實習 Stream Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer,
Exceptions. n Programmers have traditionally ignored the proper dedication of attention to error handling n Enclosing code inside countless nested-ifs.
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 16 Exceptions,
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
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.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Exception Handling. C++ 2 Outline  Throwing and handling exceptions  Exceptions of different types  The new operator and the exceptions  Re-throwing.
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.
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Exception Handling Fall 2008 Dr. David A. Gaitros
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Exception Handling Outline 23.1 Introduction
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.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
CHAPTER 18 C – C++ Section 1: Exceptions. Error Handling with Exceptions Forces you to defend yourself Separates error handling code from the source.
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.
Exception handling.
Exception Handling C++.
Exceptions Error Handling and Recovery
Abstract Class Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for.
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.
Exceptions.
EXCEPTION HANDLING IN C++
Topics: Templates Exceptions
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.
Object Oriented Programming COP3330 / CGS5409
Exceptions with Functions
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 17 Templates and Exceptions Part 2
Advanced C++ Exception Handling
Exceptions CSCE 121 J. Michael Moore
CSC 270 – Survey of Programming Languages
Exception Handling.
Exceptions 1 CMSC 202.
Object-Oriented Programming (OOP) Lecture No. 43
Object-Oriented Programming (OOP) Lecture No. 44
Lecture 9.
Exceptions for safe programming.
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

計算機概論實習

2 A Simple Way to Handle Error if(b != 0) { cout << "a/b = " << double(a / b) << endl; } else { cout << "b can not be zero" << endl; }

3 Error (Exception) Handling in C++ Exception handling is a mechanism that separates code that detects and handles exceptional circumstances from the rest of your program. Note that an exceptional circumstance is not necessarily an error. try, throw, and catch, the C++ keywords that support exception handling. try blocks: a block of code that may throw an exception that you want to handle with special processing catch blocks or handlers: a block of code that is executed when a try block encounters an exception throw expression: indicates when your program encounters an exception

4 The Model of Exception Handling

5 Using try, catch, and throw The formal representation: try { // The program which may occur exception …………… throw exception; …………… } catch (type exception){ // The action when exception occurs }

6 int main() { int a = 0, b = 0; cout << "please input a:"; cin >> a; cout << "please input b: "; cin >> b; try { if(b == 0) throw 0; cout << "a / b = " (a) / b << endl; } catch(int err) { cout << "because b is: " << err << endl; cout << "The calculation result is infinite" << endl; } return 0; } Throw exception 0 caught by err

7 please input a: 10 please input b: 0 because b is: 0 The calculation result is infinite

8 int main() { int a = 0, b = 0; cout << "please input a:"; cin >> a; cout << "please input b: "; cin >> b; try { if(b == 0) throw "Error!!, the b is zero"; cout << "a / b = " (a) / b << endl; } catch(int err) { cout << "because b is: " << err << endl; cout << "The calculation result is infinite" << endl; } catch(const char* str) { cerr << “Error: " << str << endl; } return 0; }

9 please input a: 10 please input b: 0 Error!!, the b is zero

10 Exception Specification Exception specification: void MyFunc() throw(type){ throw exception (with the same type): } try{ MyFunc(); } catch(type exception){ //error handling }

11 Exception Specification void throwInt() throw(int) { cout << "Will throw an int " << endl; throw(1); } int main() { try { throwInt(); }catch (int){ cout << "Caught an int " << endl; } system("PAUSE"); return 0; }

12 Will throw an int Caught an int

13 Standard Exception

14 Standard Exception #include // 包含 std::exception 類別的標頭檔 try { // 可能會發生例外的程式碼,如除法 } catch (std::exception& e){ // 當例外發生的時候,相對應的動作 } Standard Exception Occurs

15 Discussion You can throw an exception in catch(){} catch( int err) { // exception handle throw; // throw a exception again } If the data type of catch block does not match that of the throw exception, then program will call terminate() to abort this program automatically. If you try to catch all data type of throw exception, you can do catch(…) catch(...) { // All exceptions will be handled here }

16 Programming an Exception by Yourself See Sample.zip

17 Practice 8 (P8P PLEASE write a program that when you fail to open a file, you have to throw a exception. Directly using try, catch, and throw Define a openfile() function that will throw exception when this function occurs "can not open a file"