Skip to content

Instantly share code, notes, and snippets.

@evenchange4
Last active December 27, 2015 20:18
Show Gist options
  • Save evenchange4/7383059 to your computer and use it in GitHub Desktop.
Save evenchange4/7383059 to your computer and use it in GitHub Desktop.
Exception
// 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
int main()
{
try // 試試看(try)呼叫這個 function
{
myMethod(6);
}
catch (MyException e) // 阿,假如呼叫的過程中,有任何錯誤狀況(實作的地方丟出(throw)訊息),就把它接起來(catch)。
{
cout << e.what();
}
}
@evenchange4
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment