| 
 
 异常:是用来显示异常的一种方法。  try{  …  …  …  }catch(){  显示异常。  }  它实现的时候。当try里面的正常运行的代码遇到异常的时候,就会,不执行接下来的语句。这就类似与go to 语句。  
它可以解决以下问题:  
#include <iostream> 
#include <stdlib.h>
using namespace std;
int main(){
	
	int a = atoi("abc");   
	int b = atoi("0");
	cout << a;
	cout << b;
}
这会导致俩条语句打出来的是一样的。
  
但是编译器怎么知道什么时候是异常呢??  我们可以自己定义异常然后再用throw抛出异常  
代码:  
#include <iostream> 
#include <stdlib.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 a = myatoi("abc");   
		cout << a << endl;
	}catch(Argexception a){
		
		cout << a.what() << endl;
	}
}
  
  
                
                
                
        
    
 
 |