zoukankan      html  css  js  c++  java
  • 第65课.c++中的异常处理(下)

    1.catch语句中抛出异常

    eg:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    /*
        假设: 当前的函数式第三方库中的函数,因此,我们无法修改源代码
        
        函数名: void func(int i)
        抛出异常的类型: int
                            -1 ==》 参数异常
                            -2 ==》 运行异常
                            -3 ==》 超时异常
    */
    void func(int i)
    {
        if(i < 0)
        {
            throw -1;
        }
        
        if(i > 100)
        {
            throw -2;
        }
        
        if(i == 11)
        {
            throw -1;
        }
        
        cout << "Run func..." << endl;
    }
    
    void Myfunc(int i)
    {
        try
        {
            func(i);
        }
        catch(int i)
        {
            switch(i)
            {
                case -1:
                    throw "Invalid Parameter";
                    break;
                case -2:
                    throw "Runtime Exception";
                    break;
                case -3:
                    throw "Timeout Exception";
                    break;
            }
        }
    }
    
    int main()
    {
        try
        {
            Myfunc(11);
        }
        catch(const char* cs)
        {
            cout << "Exception Info: " << cs << endl;
        }
        
        return 0;
    }
    

    2.异常处理中自定义类类型

    a.异常的类型可以是自定义的类类型
    b.对于类类型异常的匹配依旧是自上而下,严格匹配
    c.在定义catch语句块时,如果适用的是类对象推荐使用引用。可以提高效率(没有拷贝)
    d.赋值兼容性原则在异常匹配中依然适用(即子类可以被当成父类用,这样也就会使catch本来应该匹配父类的,结果把子类匹配到父类位置)
    解决办法:
    1).匹配子类异常的catch放在上部
    2).匹配父类异常的catch放在下部

    eg:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Base
    {
    };
    
    class Exception : public Base
    {
    	int m_id;
    	string m_desc;
    public:
    	Exception(int id, string desc)
    	{
    		m_id = id;
    		m_desc = desc;
    	}
    	
    	int id() const
    	{
    		return m_id;
    	}
    	
    	string description() const
    	{
    		return m_desc;
    	}
    };
    
    
    /*
    	假设: 当前的函数式第三方库中的函数,因此,我们无法修改源代码
    	
    	函数名: void func(int i)
    	抛出异常的类型: int
    						-1 ==》 参数异常
    						-2 ==》 运行异常
    						-3 ==》 超时异常
    */
    void func(int i)
    {
    	if( i < 0 )
    	{
    		throw -1;
    	}
    	
    	if( i > 100 )
    	{
    		throw -2;
    	}
    	
    	if( i == 11 )
    	{
    		throw -3;
    	}
    	
    	cout << "Run func..." << endl;
    }
    
    void MyFunc(int i)
    {
    	try
    	{
    		func(i);
    	}
    	catch(int i)
    	{
    		switch(i)
    		{
    			case -1:
    				throw Exception(-1, "Invalid Parameter");
    				break;
    			case -2:
    				throw Exception(-2, "Runtime Exception");
    				break;
    			case -3:
    				throw Exception(-3, "Timeout Exception");
    				break;
    		}
    	}
    }
    
    int main(int argc, char *argv[])
    {
    	try
    	{
    		MyFunc(11);
    	}
    	catch(const Exception& e)
    	{
    		cout << "Exception Info: " << endl;
    		cout << "   ID: " << e.id() << endl;
    		cout << "   Description: " << e.description() << endl;
    	}
    	catch(const Base& e)
    	{
    		cout << "catch(const Base& e)" << endl;
    	}
    	
    	return 0;
    }
    

    3.c++标准库中异常类族

    标准库中的异常都是从exception类中派生的
    exception类有两个主要分支:

    logic_error

     常用于程序中的可避免逻辑错误

    runtime_error

     常用于程序中无法避免的恶性错误

  • 相关阅读:
    Android 如何在自己的应用中模拟Home键
    ComboBox添加Item
    修复JSCRIPT
    几天没用TFS!挂了!
    简单的上传图片并生成缩略图
    上传图片时中文路径的问题
    获取DataList和Repeater里的控件
    MIME 类型列表
    STL注意
    MFC下的MessageBox使用_附带CBUTTON
  • 原文地址:https://www.cnblogs.com/huangdengtao/p/12034065.html
Copyright © 2011-2022 走看看