命名空间与异常

来源:互联网 发布:mac创建acc版本在哪里 编辑:程序博客网 时间:2024/05/18 13:26

命名空间:

#include<iostream>
#include<string>
using namespace std;
namespace zhang{
 void show(){
  cout<<"hello,welcome to use namespace zhang"<<endl;
 }
}
namespace li{
 void showtoo(){
  cout<<"use namespace rencheng showone"<<endl;
 }
}
using namespace zhang;
using namespace li;
int main(){
     show();
  showtoo();
 return 0;
}

异常:

#include<iostream>
#include<string>
using namespace std;
class Bad{
public:
 Bad(string s){
  name=s;
 }
 void show(){
  cout<<name<<endl;
 }
private:
 string name;
};
int to_int(char a){
 if(a=='o')throw Bad("这是一个异常");
 return (int)a;
}
int main(){
 
 int c='o';
 try{
 
  int x=to_int(c);
 }catch(Bad b){
  b.show();
 }
 return 0;
}
#include<iostream>
#include<string>
using namespace std;
void f(){
 throw "Boom";
}
int main(){
 try{
  f();
 }catch(const char* s){
  cout<<"caught "<<s<<endl;
 }
 return 0;
}