一、简单介绍 :throw扔了什么,catch就接受什么
try{
throw 1;
throw "hello world";
}
catch(int i){
cout<<i<<endl;
}
catch(const char* s){//const 必不可少,char*不能换成string
cout<<s<<endl;
}
二、完整示例
#include<iostream>
using namespace std;
void f(int a,int b){
if(b==0){
throw runtime_error("b==0");
}
else{
cout<<"c = "<<a/b<<endl;
}
}
int main(){
int a = 12,b = 0;
try
{
f(a,b);
}
catch(const exception &e)
{
cout<<e.what()<<endl;
}
return 0;
}