Last active
December 27, 2015 20:18
-
-
Save evenchange4/7383059 to your computer and use it in GitHub Desktop.
Exception
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Class "MyException" | |
class MyException: public exception | |
{ | |
... | |
}; | |
// Class "A" | |
class A | |
{ | |
public: | |
void myMethod(int x) throw(MyException); | |
}; | |
// implement "myMethod" | |
void A::myMethod(int x) throw(MyException) | |
{ | |
// 實作階段,考慮好所有可能出錯的狀況,對應這個錯誤狀況應該要丟( throw)什麼訊息出去。 | |
if ("出錯的狀況1") | |
throw MyException("MyException: 阿,出現錯誤狀況1"); | |
... | |
} // end myMethod | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main() | |
{ | |
try // 試試看(try)呼叫這個 function | |
{ | |
myMethod(6); | |
} | |
catch (MyException e) // 阿,假如呼叫的過程中,有任何錯誤狀況(實作的地方丟出(throw)訊息),就把它接起來(catch)。 | |
{ | |
cout << e.what(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
範例程式:
Stack list ADT (stack.cpp)