NDK-CPP语言-异常处理+IO流

46 篇文章 1 订阅
42 篇文章 0 订阅


#include <iostream>
#include <string>

// using namespace std;

//2.C++语言:异常处理
//2.1.C++语言:异常处理-语法-方法内部处理异常
// int main(){
//     //规定性别0和1(0代表男,1代表女)
//     int sex = 0;
//     //规定0-200
//     int age = -300;
//     try{
//         if(sex < 0){
//             //抛异常(关键字:throw type---type是抛异常类型)
//             //和Java类似
//             throw sex;
//         }
//         if(age > 200){
//             string error = "老妖怪";
//             throw error;
//         }
//         if(age < 0){
//             double error = 100.56;
//             throw error;
//         }
//     }catch(int e){
//         //处理异常
//         cout << "无法识别性别,泰国人妖" << endl;
//     }catch(string e_c){
//         cout << "年龄不符合" << endl;
//     }catch(...){
//         cout << "未知错误" << endl;
//     }
    
//     return 0;
// }


//2.2.C++语言:异常处理-向外抛异常
// int func(int a,int b) {
//     if(b == 0){
//         string str = "除数不能够为0";
//         throw str;
//     }
//     return a / b;
// }
// int main(){
//     try{
//         func(10,0);
//     }catch(string str){
//         cout << str << endl;
//     }
//     return 0;
// }

//2.3 C++语言:异常处理-向外抛异常-指定异常类型
// int func(int a,int b) throw(string){
//     if(b == 0){
//         string error = "除数不能够为0";
//         throw error;
//     }
//     return a / b;
// }
// int main(){
//     try{
//         func(10,0);
//     }catch(string e){
//         cout << e << endl;
//     }
//     return 0;
// }


//2.3 C++语言:异常处理-抛出对象异常
//在Java中我们不能够抛出基本数据类型的异常
//但是在C++中就可以抛出
// class Exception{
// private:
//     string error;
// public:
//     //构造函数
//     Exception(string error){
//         this->error = error;
//         cout << "构造函数" << endl;
//     }
//     //拷贝函数
//     Exception(const Exception &exception){
//         this->error = exception.error;
//         cout << "拷贝函数" << endl;
//     }
//     //析构函数
//     ~Exception(){
//         cout << "析构函数" << endl;
//     }

//     void print(){
//         cout << this->error << endl;
//     }
// };

// int func(int a,int b){
//     if(b == 0){
//         //这种写法是抛对象
//         //以下写法创建了了两个对象
//         // throw Exception("除数不能够为0");
//         //解决方案
//         //方案一:传递引用
//         // throw Exception("除数不能够为0");
//         //方案二:传递指针
//         //该写法存在问题:传递的是指针,
//         //对象的生命周期只在当前方法中,指针对应的对象被回收了,变成了野指针
//         // Exception e = Exception("除数不能够为0");
//         // return &e;
//         //以下正确写法
//         //以下写法是动态内存分配,需要自己手动的释放内存
//         // Exception* e = new Exception("除数不能够为0");
//         //最终总结:传递引用是最为合适的选择
//         throw throw Exception("除数不能够为0");
//     }
//     return a / b;
// }

// int main(){
//     // //捕获异常(对象传递)
//     // try{
//     //     func(10,0);
//     // }catch(Exception e){
//     //     e.print();
//     // }

//     //捕获异常(引用传递)
//     // try{
//     //     func(10,0);
//     // }catch(Exception &e){
//     //     e.print();
//     // }

//     //捕获异常(指针传递)
//     // try{
//     //     func(10,0);
//     // }catch(Exception* e){
//     //     e->print();
//     //     delete e;
//     // }
//     return 0;
// }



//2.4 C++语言:异常处理-内部类
// class Error{
//     class Exception{
//         private:
//             string error;
//         public:
//             Exception(string error){
//                 this->error = error;
//             }
//     }
// }

// int main(){
//     Error::Exception exception("异常");
//     return 0;
// }



//2.5 C++语言:异常处理-异常继承
//基类
// class Exception{
//     private:
//         string error;
//     public:
//         Exception(string error){
//             this->error = error;
//         }

//         void print(){
//             cout << this->error << endl;
//         }
// };
// //空指针异常
// class NullPointerException : public Exception{
//     public:
//         NullPointerException(string error) : Exception(error){

//         }
// };
// //参数异常
// class IllegalArgumentException : public Exception{
//     public:
//         IllegalArgumentException(string error) : Exception(error){

//         }
// };

// int func(int a,int b) throw(NullPointerException,IllegalArgumentException){
//     if(b == 0){
//         throw NullPointerException("空指针异常");
//     }
//     if(a < 0){
//         throw IllegalArgumentException("参数异常");
//     }
//     return a / b;
// }

// int main(){
//     // try{
//     //     func(-10,10);
//     // }catch(NullPointerException &e){
//     //     e.print();
//     // }catch(IllegalArgumentException &e){
//     //     e.print();
//     // }

//     try{
//         func(-10,10);
//     }catch(Exception &e){
//         e.print();
//     }
//     return 0;
// }



//2.6 C++语言:异常处理-标准异常-系统自带的
//引入标准异常库
// #include <stdexcept>
// //所有的标准异常基类都是exception类
// int main(){
//     // int i = 200;
//     // try{
//     //     if(i == 100){
//     //         throw invalid_argument("参数异常");
//     //     }
//     //     if(i == 200){
//     //         throw out_of_range("空指针异常");
//     //     }
//     // }catch(invalid_argument &arg){
//     //     cout << arg.what() << endl;
//     // }catch(out_of_range &e_out){
//     //     cout << e_out.what() << endl;
//     // }

//     int i = 100;
//     try{
//         if(i == 100){
//             throw invalid_argument("参数异常");
//         }
//         if(i == 200){
//             throw out_of_range("空指针异常");
//         }
//     }catch(exception &e){
//         cout << e.what() << endl;
//     }
//     return 0;
// }


//2.7 C++语言:异常处理-标准异常-自定义异常
// #include <stdexcept>

// using namespace std;
// //捕获应用程序异常:Android
// class null_pointer_exception : public logic_error{
//         //扩展功能,覆盖父类方法,定制自己的功能
//     public:
//         null_pointer_exception(string error) : logic_error(error){

//         }
// };

// int main(){
//     int a = 100;
//     try{
//         if(a == 100){
//             throw null_pointer_exception("空指针异常");
//         }
//     }catch(null_pointer_exception &e){
//         cout << e.what() << endl;
//     }
//     return 0;
// }



//3 C++语言:IO流
//3.1 C++语言:IO流-文本文件-写入内容
#include <fstream>
using namespace std;
// int main(){
//     //文件路径
//     string f_path = "/Users/yangshaohong/Desktop/io.txt";
//     //创建IO流:写入(out of stream)
//     ofstream f_out(f_path);
//     //判断是否打开成功
//     if(f_out.bad()){
//         cout << "打开失败" << endl;
//         return 1;
//     }
//     //写入内容
//     f_out << "Dream" << endl;
//     f_out << "潭州教育" << endl;
//     //关闭流
//     f_out.close();
//     return 0;
// }


//3.2 C++语言:IO流-文本文件-读取内容
// int main(){
//     //文件路径
//     string f_path = "/Users/yangshaohong/Desktop/io.txt";
//     //读(in of stream)
//     ifstream f_in(f_path);
//     if(f_in.bad()){
//         cout << "打开文件失败" << endl;
//         return 1;
//     }
//     char r;
//     while(f_in.get(r)){
//         cout << r;
//     }
//     //关闭流
//     f_in.close();
//     return 0;
// }

//3.3 C++语言:IO流-文本文件-写入内容-追加
// int main(){
//     //文件路径
//     string f_path = "/Users/yangshaohong/Desktop/io.txt";
//     //创建IO流:写入(out of stream)
//     //ios::app (append追加)
//     ofstream f_out(f_path,ios::app);
//     //判断是否打开成功
//     if(f_out.bad()){
//         cout << "打开失败" << endl;
//         return 1;
//     }
//     //写入内容
//     f_out << "Dream" << endl;
//     f_out << "潭州教育" << endl;
//     //关闭流
//     f_out.close();
//     return 0;
// }


//3.4 C++语言:IO流-二进制文件-读写
// int main(){
//     //原始路径
//     string src_path = "/Users/yangshaohong/Desktop/常量类型转换.png";
//     //拷贝路径
//     string copy_path = "/Users/yangshaohong/Desktop/copy.png";

//     //创建输入流(操作二进制文件: ios::binary)
//     ifstream f_in_src(src_path,ios::binary);
//     //创建机输出流
//     ofstream f_out_copy(copy_path,ios::binary);
    
//     if(f_in_src.bad() || f_out_copy.bad()){
//         cout << "文件打开失败" <<endl;
//         return 1;
//     }

//     //首先读取至缓冲区
//     char buffer[1024] = {0};
//     //判断是否读取到了最后
//     while(!f_in_src.eof()){
//         //读取
//         f_in_src.read(buffer,1024);
//         //写入
//         f_out_copy.write(buffer,1024);
//     }
//     //关闭流
//     f_in_src.close();
//     f_out_copy.close();
//     return 0;
// }


//3.5 C++语言:IO流-二进制文件-对象持久化
//类似于Java中的序列化
class Company{
    private:
        string name;
    public:
        Company(){
            this->name = "阿里巴巴";
            cout << "构造函数" << endl;
        }
        Company(string name){
            this->name = name;
            cout << "构造函数" << endl;
        }
        Company(const Company &company){
            this->name = company.name;
            cout << "拷贝函数" << endl;
        }

        ~Company(){
            cout << "析构函数" << endl;
        }

        void print(){
            cout << "潭州" << endl;
        }
};

void func(){
    string src_path = "/Users/yangshaohong/Desktop/Company.data";
    Company temp;
    ifstream f_in(src_path,ios::binary);
    f_in.read((char*)(&temp),sizeof(Company));

    temp.print();

    // f_in.close();
}

int main(){
    
    //持久化对象(类似于Java中的序列化)
    Company company = Company("潭州教育科技有限公司");
    string src_path = "/Users/yangshaohong/Desktop/Company.data";
    //输出流
    ofstream f_out(src_path,ios::binary);
    f_out.write((char*)(&company),sizeof(Company));
    f_out.close();

    //读取对象状态(类似于Java中的反序列化)
    func();

    return 0;
}


整理自示例代码



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值