C ++中的一般异常处理 [英] Generic exception handling in C++

查看:107
本文介绍了C ++中的一般异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,您可以使用:

  try:
#some code
except Exception as e :
print e

这将自动捕获任何异常。假设你想在'#一些代码 ...

 变量中做一个简单的划分= int(raw_input('Input:'))
result = 50 / variable

,想像有人输入0,或一个字母,甚至一个字符串...据我所知,由于我们使用泛型异常捕获,所有这一切将被处理,因此代码将跳转到除了块,避免崩溃。






在C ++中,您必须检查输入是否在一个IF语句中的数字,然后检查它在另一个不是零,如果是这样的话,抛出相应的异常/ s。

  int var = 0,result = 0; 

try {
cin>> var;

if(var == 0)
throw分区错误;

result = 50 / var;

} catch(...){
//异常处理代码
}

所有这一切,(虽然不整洁,一个不好的做法),可以简单地用纯粹的IF-ELSE语句完成,而没有使用 try-catch 块。但是,在C ++中,如果任何意外的异常发生,就完成了。您必须手动处理每个异常,为每种可能的情况添加 throw 语句,而在python中,这是自动为您完成的,甚至会告诉您出了什么问题,以及什么样的异常您的通用变量'e'被捕获。






所以问题是:

有没有办法在任何版本的C ++中编写一个try-catch 块,它会自动处理任何异常并将其分配给一个通用变量供您打印? / strong>



因为如果没有,我没有看到使用 try-catch 结构的目的,除了使你的代码看起来整齐,更有组织(与switch语句相同)。

解决方案

关于高级语言如python 检测异常,这一切都在于所有事物都是一个对象(甚至是简单的int和chars)以及每个操作因此,例如,如果您尝试除以零,解释器(而不是C ++的编译器,只是进行适当的明智的操作,使其更快,但更基本) strong>将操作作为一个整体预定义的方法,并相应地执行。重要的是,由Python中定义的那些方法本身已经有所有必需的检查,引起相应的异常。



如果您花费时间来重载所有内容,并确保您处理的每个变量实际上都是一个对象,并且其所有方法和操作符都执行所有相应的检查,抛出一个通用异常(该确保定义一个默认的泛型异常类,派生自std :: exception类,并覆盖相应的方法来实现所需的结果,就像@Brian所说),如果需要,你可以实现几乎相同的结果。 p>

In python you can use:

try:
    #some code
except Exception as e:
    print e

And this will automatically catch any exception. Lets say you want to make a simple division in '#some code"...

variable = int(raw_input('Input: '))
result = 50/variable

Now, imagine somebody enters 0, or a letter, or even a string... As far as I know, since we used the generic exception catch, all of this will be taken care of and thus the code will jump to the except block, avoiding a crash.


In C++, you would have to check that the input is a number in one IF statement, and then check that it is not zero in another one, and if that's the case, throw the corresponding exception/s.

int var = 0, result = 0;

try{
    cin >> var;

    if (var == 0)
        throw "Zero Division Error";

    result = 50/var;

}catch(...){
    //exception handling code
}

All of this, (although untidy, and a bad practice), could have been done simply with pure IF-ELSE statements, without the try-catch block. However, in C++, should any unexpected exception occur, you are done. You have to manually take care of every exception, adding throw statements for every possible case, whereas in python, this is done automatically for you, and it even tells you what went wrong, and what kind of exception your generic variable 'e' caught.


So the question is:
Is there a way to write a try-catch block in whatever version of C++, that automatically takes care of any exception and assign it to a generic variable for you to print?

Because if there is not, I don't see the purpose of using the try-catch structure, other than making your code look tidy and more organized (same with the switch statement).

解决方案

Regarding the fact that high level languages like python "automatically" detect exceptions, it all lies upon the fact that everything is an object (even simple ints and chars), and every operator is overloaded accordingly, so, for example, if you try to divide by zero, the interpreter (as opposed to C++'s compiler which simply does the appropriate bit wise operations, making it faster but more basic) takes the operation as a whole predefined method, and executes it accordingly. The important thing is that Inside Those methods predefined by python itself, there already are all the required checks that raise the corresponding exceptions.

If you take the time to overload everything and make sure that every variable you treat is actually an object, and all its methods and operators do the all corresponding checking, throwing a generic exception (that is making sure to define a default generic exception class, derived from the std::exception class, and overriding the corresponding methods to achieve your desired result, just like @Brian said) if necessary, you can achieve practically the same result.

这篇关于C ++中的一般异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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