int main(int argc, char *argv[])
{
int num = 10 / 0;
cout << "OVER" << endl;
return 0;
}
//不会显示OVER,程序异常结束
语法:throw 值或变量;
例如:
语法:
#include
#include
using namespace std;
//异常步骤,抛出异常,捕获异常
int mydiv(int a,int b)
{
if(b == 0)
{
// 抛出异常
int num = 0;
throw num;
}
return a / b;
}
void test01(){
try{
mydiv(10,0);
}
catch(int e)
{
cout << e << endl;
}
catch(char const* s)
{
cout << s << endl;
}
catch(...)
{
cout << "其他异常" << endl;
}
}
int main(int argc, char *argv[])
{
test01();
return 0;
}
概念
示例
class A{
private:
int num;
public:
A(int num):num(num)
{
cout << "构造函数" << num << endl;
}
~A()
{
cout << "析构函数" << num << endl;
}
};
void test02()
{
A a1(1);
A a2(2);
throw 0;
}
int main(int argc, char *argv[])
{
try{
test02();
}
catch(...)
{
}
return 0;
}
结果
作用
语法
示例
void fun01()throw(int,char)
{
// throw 10;//可以
// throw 'a';//可以
// throw 3.14f;//不可以
}
void test03(){
try{
fun01();
}
catch(int)
{
cout << "int的异常" << endl;
}
catch(char)
{
cout << "char的异常" << endl;
}
catch(float)
{
cout << "float的异常" << endl;
}
}
int main(int argc, char *argv[])
{
test03();
return 0;
}
示例1:抛出异常对象
#include
#include
using namespace std;
class B{
private:
int num;
public:
B(int num):num(num)
{
cout << "构造函数" << num << endl;
}
B(const B& b)
{
this->num = b.num;
cout << "拷贝构造" << num << endl;
}
~B()
{
cout << "析构函数" << num << endl;
}
};
void fun02()
{
throw B(10);
}
void test04()
{
try
{
fun02();
}
catch(B b)
{
}
}
int main(int argc, char *argv[])
{
test04();
cout << "OVER" << endl;
return 0;
}
结果
示例2:抛出异常对象指针
#include
#include
using namespace std;
class B{
private:
int num;
public:
B(int num):num(num)
{
cout << "构造函数" << num << endl;
}
B(const B& b)
{
this->num = b.num;
cout << "拷贝构造" << num << endl;
}
~B()
{
cout << "析构函数" << num << endl;
}
};
void fun02()
{
throw new B(10);
}
void test04()
{
try{
fun02();
}
catch(B *b)
{
}
}
int main(int argc, char *argv[])
{
test04();
cout << "OVER" << endl;
return 0;
}
示例3:抛出异常对象引用
#include
#include
using namespace std;
class B{
private:
int num;
public:
B(int num):num(num)
{
cout << "构造函数" << num << endl;
}
B(const B& b)
{
this->num = b.num;
cout << "拷贝构造" << num << endl;
}
~B()
{
cout << "析构函数" << num << endl;
}
};
void fun02()
{
throw B(10);
}
void test04()
{
try{
fun02();
}
catch(B &b)
{
}
}
int main(int argc, char *argv[])
{
test04();
cout << "OVER" << endl;
return 0;
}
结果:
class BaseException{};
class MyException01:public BaseException{};
class MyException02:public BaseException{};
void test05()
{
try{
throw MyException01();
}
catch(BaseException)
{
cout << "可以捕获子类异常" << endl;
}
}
int main(int argc, char *argv[])
{
test05();
return 0;
}
class BaseException{
public:
virtual void printMsg(){}
};
class NullException:public BaseException{
public:
virtual void printMsg(){
cout << "空指针异常" << endl;
}
};
class ArrOutException:public BaseException{
public:
virtual void printMsg(){
cout << "数组下标越界异常" << endl;
}
};
void test05()
{
try{
throw NullException();
}
catch(BaseException &e)
{
e.printMsg();
}
}
int main(int argc, char *argv[])
{
test05();
return 0;
}
void test06()
{
try{
throw bad_alloc();
}
catch(exception &e)
{
cout << e.what() << endl;
}
}
int main(int argc, char *argv[])
{
test06();
return 0;
}
步骤
示例
class my_exception:public exception
{
private:
char* msg;
public:
my_exception()
{
}
my_exception(char* msg)
{
this->msg = msg;
}
const char *what()const noexcept
{
return msg;
}
};
void test07()
{
try{
throw my_exception("自定义异常");
}
catch(exception &e){
cout << e.what() << endl;
}
}
int main(int argc, char *argv[])
{
test07();
return 0;
}