CException
is the base type of all exceptions thrown by VC++, so it should catch all the exceptions, right?
Catch and Catch (Exception e) have the same output, and the result is also the same if I write Throw or Throw e. Both are the same because you throw . @MarcGravell - Marc yes if he throws e there is a difference. Got it, I just wanted to re-tag but I see that has already been taken care of!
The try statement allows you to define a block of code to be tested for errors while it is being executed. The throw keyword throws an exception when a problem is detected, which lets us create a custom error. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
Catch (Exception ex) catches all exceptions and in addition you can retrieve message through its reference. Use is dependent on requirement, if you want to show exception message you have facility to use ex. Message otherwise Catch (Exception) will be enough.
To catch exceptions, a portion of code is placed under exception inspection. This is done by enclosing this portion of code in a try block. When an exception occurs within the try block, control is transferred to the exception handler. If no exception is thrown, the code continues normally and the handlers are ignored.
CException
is not the base type for all extensions (it might be the base type of all exceptions that MFC code uses, but that's as far as it goes).
In C++, you can throw anything; it doesn't have to be an "exception" subclass, and it does not even have to be an object. It's perfectly legal for example to write throw 42;
or throw new std::vector<string>()
;
The difference is then obvious: catch(CException)
will catch only thrown instances of CException
and its subclasses, while the other will catch anything.
C++ exceptions can be of any type. With a catch() you specify the type of the exception you want to catch as a parameter. The special case with (...) is that you will catch any other exception you didn't specified in the earlier catches.
The base of exceptions in C++ for the Standard library is std::exception. To catch all exceptions, the standard way in a C++ program is the following:
(in a main function for instance)
try
{
}
catch( const std::exception & e )
{
// catch standard exceptions, you can use e.what() to know what exception you caught
}
catch( ... )
{
// catch all other types but you can't do much with them
}
It is not recommended, even if it's possible, to throw your own exceptions that do not inherit from std::exception. But CException do not seem to inherit from it.
In your case, I'll advise you to do the following to catch all exceptions that might arise (again, for instance in your main function and in the main thread functions):
try
{
}
catch( const CException & e )
{
// catch all CExceptions
//as far as I know it is ok now to catch CException by reference with modern Microsoft compilers? It was not always the recommended microsoft way
}
catch( const std::exception & e )
{
// catch standard C++ exception
}
catch( ... )
{
// catch others
}
As always, when not used to it, it may be tricky to know what is standard C++ and what is windows API.
throw "ex";
will actually throw a string (well, const char*
, but you know what I mean).
catch (TYPE t)
will only catch objects of type TYPE (doesn't have to be an exception).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With