Reputation: 1075
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).
Upvotes: 0
Views: 3498
Reputation:
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.
Upvotes: 0
Reputation: 119641
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?
Yes if you follow the practice of only throwing exceptions of class type derived publicly from std::exception
and overridding the what()
method so that it returns a string with some appropriate error message. Any exception can then be handled with:
try {
// ...
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
For example, you could write an exception class like this:
class divide_error : public std::exception {
const char* what() const {
return "division by 0";
}
};
The question of what exactly exceptions are good for in C++ is more opinion-based and has been covered elsewhere. See When and how should I use exception handling?
Upvotes: 3