Download presentation
Presentation is loading. Please wait.
1
計算機概論實習 2007-06-01
2
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
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
4 The Model of Exception Handling
5
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
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
7 please input a: 10 please input b: 0 because b is: 0 The calculation result is infinite
8
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
9 please input a: 10 please input b: 0 Error!!, the b is zero
10
10 Exception Specification Exception specification: void MyFunc() throw(type){ throw exception (with the same type): } try{ MyFunc(); } catch(type exception){ //error handling }
11
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
12 Will throw an int Caught an int
13
13 Standard Exception
14
14 Standard Exception #include // 包含 std::exception 類別的標頭檔 try { // 可能會發生例外的程式碼,如除法 } catch (std::exception& e){ // 當例外發生的時候,相對應的動作 } Standard Exception Occurs
15
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
16 Programming an Exception by Yourself See Sample.zip
17
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"
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.