Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ catch(std::exception & e ) vs. catch(...)

I know the difference in handling of both of these catches, but what does it take for the ellipse to catch something the std::exception catch wouldn't catch?

For example:

try
{
    throw std::runtime("runtime error!");
}
catch(const std::exception& e)
{
    std::cout << "Exception: " << e;
}
catch(...)
{
    std::cout << "How did I get here?";
    throw;
}

I've seen examples of code that use both of these in conjunction, but I've not seen a reason you would do both.

like image 971
felix1415 Avatar asked Apr 17 '18 12:04

felix1415


People also ask

Does catch catch all exceptions C++?

Catch block is used to catch all types of exception. The keyword “catch” is used to catch exceptions.

How do you catch an exception in C++?

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.

Can we throw exception in catch?

When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects). Or, wrap it within a new exception and throw it.

How do you handle exceptions in catch?

The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.

How do you catch exceptions in C++?

The following steps are needed to catch all the exceptions in C++: Declare a class to be used as the exception handler. Define what exceptions should be caught by this handler. Have the main function call the new C++11 exception mechanism with an instance of the class used to catch exceptions.

Does the CATCH () method increase the amount of exceptions you can catch?

It also doesn't increase the types of exceptions that can be caught, compared to catching by value ( catch (std::exception e) without the & - you'll still catch each exception that either is std::exception or derives from it). What it increases is the amount of data that you will actually get when you catch the exception.

How do you catch exceptions in a try block?

To catch exceptions, a portion of code is placed under exception inspection. This is done by enclosing that portion of code in a try-block. When an exceptional circumstance arises within that block, an exception is thrown that transfers the control to the exception handler.

What is the new exception mechanism in C++?

The new current exception mechanism is a way to catch all exceptions in C++; it was introduced in C++11. It is an alternative to the old-fashioned technique of catching only some exceptions with try-catch blocks. This new method has some features that make it preferable to the old one.


2 Answers

catch(const std::exception& e)

Will catch std exceptions only.

catch(...)

Will catch everything there after.

You can handle integers and other types (http://www.cplusplus.com/doc/tutorial/exceptions/)

For example:

catch(int e)
like image 65
felix1415 Avatar answered Sep 22 '22 08:09

felix1415


While it's definitely a good idea to do so, you don't have to derive your custom exceptions from std::exception. C++ allows you to throw practically any object type.

So throw 1; will not be handled by your first handler, for example. And neither will...

class MyCustomException { // Doesn't derive
 ///
};

... if it was thrown.

like image 34
StoryTeller - Unslander Monica Avatar answered Sep 22 '22 08:09

StoryTeller - Unslander Monica