Taday——异常处理(2)

本文详细介绍了Python中异常处理的基础,包括try{}

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、捕获异常

1.异常处理机制的原理
在真正导致错误的语句发送之前,并且异常发生的条件已经具备了,使用我们自定义的的软件异常或者系统的软件异常来代替它,从而阻止它,因此当异常抛出时,真正的错误实际上还没有发生。

2.异常处理机制的语法结构
try{}块包含可能会有异常抛出的代码段
catch{}块包含用户自定义的异常处理代码
throw是一条语句,用于抛出异常
需要注意的是:一条throw语句只能抛出一条异常,一个catch子句也只能捕获一种异常,异常捕获必须和try块结合使用,并且可以通过异常组合在一个地点捕获多种异常。

3.异常的类型匹配规则
1、catch子句的参数类型就是异常对象的类型或引用
2、如果catch子句类型为public基类指针,而异常对象则为派生类指针
3、catch子句参数类型为void*,而异常对象类型可为所类型
4、catch语句可以为catch(…),代表捕获所有可能的异常
5、允许const对象转为非const对象
6、将数组转化为指向数组类型的指针,将函数转化为指向函数类型的指针

class Exception
{
public:
    Exception(int errId ,  char * errMsg)
        : _errId(errId)
        , _errMsg(errMsg)
    {}
    void What() const
    {
        printf("errId:\n", _errId);
        printf("errMsg:%s\n", _errMsg);
    }
private:
    int _errId; // 错误码
    string _errMsg; // 错误消息
};
void Func1(bool isThrow)
{
    if (isThrow)
    {
        throw Exception(1, "抛出 Excepton对象");
    }
    printf("Func1(%d)\n", isThrow);
}
void Func2(bool isThrowString, bool isThrowInt)
{
    if (isThrowString)
    {
        throw string("抛出 string对象");
    }
    if (isThrowInt)
    {
        throw 7;
    }
    printf("Func2(%d, %d)\n", isThrowString, isThrowInt);
}
void Func()
{
    try
    {
        Func1(true);
        Func2(false, true);
    }
    catch (const string& errMsg)
    {
        printf("Catch string Object:", errMsg);

    }
    catch (int errId)
    {
        printf("Catch string Object:", errId);

    }
    catch (const Exception& e)
    {
        e.What();
    }
    catch (...)
    {
        cout << " 未知异常" << endl;
    }
    printf("Func()\n");
}

int main()
{
    Func();
    system("pause");
    return 0;
}

Func1和Func2中的false和true位置不一样,以及两个函数顺序不一样都会得到不一样的异常处理,根据抛出的异常匹配到最适合的异常捕获。

二、多个异常如何处理

1,声明异常时,建议声明更为具体的异常,这样可以处理的更具体

2,对方声明几个异常,就对应几个catch块, 如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面

以下实例演示了如何处理多异常:

class Demo  
{  
    int div(int a,int b) throws ArithmeticException,ArrayIndexOutOfBoundsException//在功能上通过throws的关键字声明该功能可能出现问题  
    {  
        int []arr = new int [a];  
  
        System.out.println(arr[4]);//制造的第一处异常  
  
        return a/b;//制造的第二处异常  
    }  
}  
  
  
class ExceptionDemo  
{  
    public static void main(String[]args) //throws Exception  
    {  
        Demo d = new Demo();  
  
        try  
            {  
                int x = d.div(4,0);//程序运行截图中的三组示例 分别对应此处的三行代码  
                //int x = d.div(5,0);  
                //int x = d.div(4,1);  
                System.out.println("x="+x);   
        }  
        catch (ArithmeticException e)  
        {  
            System.out.println(e.toString());  
        }  
        catch (ArrayIndexOutOfBoundsException e)  
        {  
            System.out.println(e.toString());  
        }  
        catch (Exception e)//父类 写在此处是为了捕捉其他没预料到的异常 只能写在子类异常的代码后面  
                            //不过一般情况下是不写的   
        {  
            System.out.println(e.toString());  
        }  
          
  
  
        System.out.println("Over");  
    }  
  
}

以上代码运行输出结果为:

java.lang.ArrayIndexOutOfBoundsException: 4
Over
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值