C++异常(try_catch)
语法现象
字符串转数字:
int atoi(const char *ptr);
int myatoi(const char *ptr)
{
if()//不是数字字母
throw myexceprion(“!0-9”);
……
}
调用:
try{
num = myatoi(“abcd”);
}
catch(myexception &e){
cout<<e.what();
}
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int myatoi(const char *str)
{
if(*str<'0' || *str>'9')
// printf("wrong arg!!\n");
// goto xxx;
throw "wrong arg!!!!";
else
return atoi(str);
}
int main()
{
// int data = atoi("0");
try{
int data = myatoi("asdfas");
cout<< data <<endl;
}
catch(const char *p)
{
cout<<p<<endl;
}
//xxx:
// cout<<"xxxxxxxxxxxxxxxxxxxxxxx"<<endl;
}
exception类
class exception //namespace std
{
public:
exception() throw() { }
virtual ~exception() throw();//不抛异常函数
/*Returns a C-style character string describing the general cause of the current error. */
virtual const char* what() const throw();
};
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <exception>
using namespace std;
class argexception :public exception//声明一个标准异常类 参数异常类
{
public:
const char* what() const throw()
{
return "arg Err !";
}
};
int myatoi(const char* str)
{
if (*str < '0' || *str>'9')
throw argexception();//一切皆对象 扔一个类
else
return atoi(str);//实现字符串转成整形的函数
}
int main()
{
try
{
int data = myatoi("asdfas");
cout << data << endl;
}
catch (argexception e)//捕捉argexception类的对象
{
cout << e.what() << endl;
}
}
自定义异常
class myexception : public exception
{
public:
const char* what() const throw()
{
return “xxxxxxxxxx”;
}
};
预定义异常
C++转换函数
标准转换函数
reinterpret_cast
reinterpret_cast<new type>(expression)
将一个类型的指针转换为另一个类型的指针,它也允许从一个指针转换为整数类型
const_cast
const_cast< new type>( expression)
const指针与普通指针间的相互转换,注意:不能将非常量指针变量转换为普通变量
static_cast
static_cast<new type>(expression)
主要用于基本类型间的相互转换,和具有继承关系间的类型转换
dynamic_cast
dynamic_cast<newtype>(expression)
只有类中含有虚函数才能用dynamic_cast;仅能在继承类对象间转换
dynamic_cast具有类型检查的功能,比static_cast更安全
#include <stdio.h>
#include <iostream>
#include <typeinfo>
using namespace std;
class A
{
public:
virtual void show()
{
cout << "aaaaaaa" << endl;
}
};
class B :public A
{
public:
void show()
{
cout << "bbbbbbbbbbbbaaaaaaa" << endl;
}
};
int main()
{
#if 0
int a;
// char *p = (char*)&a;
char* p = reinterpret_cast<char*>(&a);//将一个类型的指针转换为另一个类型的指针,它也允许从一个指针转换为整数类型,已经不怎么使用
#endif
#if 0
const int b = 100;
int* q = const_cast<int*>(&b);//const指针与普通指针间的相互转换,注意:不能将非常量指针变量转换为普通变量
#endif
#if 0
A a;
B& p = static_cast<B&>(a);//不安全 主要用于基本类型间的相互转换,和具有继承关系间的类型转换
#endif
try
{
A a;
B& p = dynamic_cast<B&>(a);//只有类中含有虚函数才能用dynamic_cast;仅能在继承类对象间转换 dynamic_cast具有类型检查的功能,比static_cast更安全
}
catch (bad_cast e)//用try catch捕获异常, what获取异常字符串
{
cout << e.what() << endl;
}
}
自定义转换函数
成员转换函数:
class A{
public:
A(int x):data(x){ }
operator int() const{ //无返回
reutn data;
}
Private:
int data;
};
#include <stdio.h>
#include <unistd.h>
#include <iostream>
using namespace std;
class Timer
{
public:
Timer()
{
hour = 0;
min = 0;
sec = 0;
}
~Timer()
{
}
void addtimer(int sec = 1)
{
this->min += (this->sec + sec) / 60;
this->sec = (this->sec + sec) % 60;
}
void show()
{
printf("%2d:%2d:%2d\n", hour, min, sec);
}
Timer operator+(int sec)
{
Timer tem;
tem.sec = this->sec + sec;
return tem;
}
Timer operator+(Timer& x)
{
Timer tem;
tem.sec = sec + x.sec;
tem.min = min + x.min;
tem.hour = hour + x.hour;
return tem;
}
Timer operator++(int)
{
Timer tem = *this;//backup
sec++;
return tem;
}
Timer operator++()
{
sec++;
return *this;
}
bool operator==(Timer& x)
{
if (sec == x.sec && min == x.min && hour == x.hour)
return true;
return false;
}
int& operator[](int i)
{
switch (i)
{
case 0:
return hour;
case 1:
return min;
case 2:
return sec;
}
}
operator int()//自定义强转换函数
{
return sec + min * 60 + hour * 60 * 60;
}
friend ostream& operator<<(ostream& out, const Timer& t);
private:
int hour;
int min;
int sec;
};
ostream& operator<<(ostream& out, const Timer& t)
{
out << "hour: " << t.hour << " min: " << t.min << " sec: " << t.sec << endl;
}
int main()
{
Timer t;
t.addtimer(3);
int sec = t;
cout << sec << endl;
}
隐式转换(explicit)
示例:
class Arr{
public:
Arr(int x):size(x){}
private:
int size;
};
Arr a = 123;
#include <iostream>
using namespace std;
class mempool
{
public:
explicit mempool(int size)//explicit避免不必要的强制类型转换 构造函数加隐士声明
{
data = new char[size];
cout << "cccccccccc" << endl;
}
~mempool()
{
delete[] data;
}
private:
char* data;
};
int main()
{
// mempool a(100);
mempool a = 100;
}