如何在本地cpp中捕获异常? [英] how to catch exceptions in native cpp?

查看:150
本文介绍了如何在本地cpp中捕获异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们
我对本机cpp相对熟悉,但从未使用过异常处理.
现在,我需要帮助来了解如何捕获这样的异常

Helo guys
I am relatively familiar with native cpp but never used exception handling.
Now I want help to understand how can I catch exceptions like this

try
{
    int p = 0;
    int q = 0;
    int r = p / q;//divide by zero exception
}
catch (???)//what should I write here instead of ????
{
    .....
}


try
{
    int * p = 0;
    * p = 0;
}
catch (???)//what should I write here instead of ????
{
}

对有关如何找到有用文章的任何建议表示赞赏
感谢advanmce
阿布扎德先生

由abzadeh先生附加:我发现通过try {} catch {}机制无法解决被零除和访问冲突异常的问题.我认为catch块可以捕获程序员显式抛出的执行.
现在我的问题是:

Any suggestion about how to find usefull articles is appreciated
Thanks in advanmce
mr.abzadeh

appended by mr.abzadeh:I have found that divide by zero and access violation exceptions are not handled thru try {} catch {} mecanism. I think the catch block catches exections explicitly thrown by programmer.
Now my question is:
Is there any mechanism to catch exceptions like integer divide by zero and access violation in native c++?

推荐答案

异常处理如下:

Exception handling is as follows:

try
{
 // risky code
}
catch(Excetpion_Name& ex)
{
 // handle here
}



您可以使用以下类型捕获所有异常,无论其类型如何:



You can catch all exceptions regardless their type with:

catch(...)
{

}


这篇关于如何在本地cpp中捕获异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆