1.捕获异常
2.不适用异常机制
3.抛出异常
#include <iostream>
#include <string>
#include <exception>
using namespace std;
/*
* 1.捕获异常
* 2.不适用异常机制
*3.抛出异常
* */
int getDiv(int a,int b){
if( b==0){
return 100001;
}
return a/b;
}
int calcDiv(int a,int b){
if(b==0){
throw runtime_error("除数不能为0");
}
return a/b;
}
int main() {
string str = "kljlkjlkdfjd";
try{
char ch2 = str.at(100);
cout<<ch2<<endl;
}catch (exception &e){
cout<<"2 out of bound"<<endl;
}
getDiv(1,0);
calcDiv(2,0);
return 0;
}