Hello
Can someone explain me why exception handling is used?
I have the following example:
Code:
#include "Matrix.h"
#include "iostream"
using namespace std;
class CInvalidArgumentsException{};
int main(void){
CInvalidArgumentsException e;
try{
Matrix matrix1 = Matrix(10,10);
if(test something)
throw(e);
else (do something)
}catch(CInvalidArgumentsException){
cout << "error\n";
}
return 0;
}
I know how it works, you give an id to the constructor from the class: CInvalidArgumentsException and generate an error that you ask in the catch.
What i'm not sure of is, is it necessary to always test in the try catch block with an if test to trow the exception? If this is the only way is it not easier to print the error out in the if test?
I am used to programming in C.
Thank you