What to throw when throwing C++ exceptions?

This might be kind of a silly question, but in C++, when I want to throw an exception.. what do I throw?

Am I supposed to throw std::exception, or is that reserved by the standard library? Or should I throw a string or int? Or should I just throw whatever I feel is appropriate?

what are alternatives to throwing exceptions in objective c

Google Coding Standard for Objective C says we should not throw exceptions, what are the alternatives to @throw then?

C# Re-throwing Exceptions

When throwing exceptions between multiple methods, should all methods re-throw the exception? for example Method1() { Method2(); } Method2() { try { // Do something } catch { throw; } } try { Method1

When to throw exceptions?

Exceptions are wonderful things, but I sometimes worry that I throw too many. Consider this example: Class User { public function User(user){ // Query database for user data if(!user) throw new Exis

When methods are throwing exceptions in jUnit

What do you do with methods that throw exceptions in jUnit tests? As you see the addAnswer method in the Question class may throw a exception. In the shouldFailifTwoAnswerAreCorrect method I want to c

C++ -The right way to throw and catch exceptions [duplicate]

Possible Duplicate: throwing exceptions of objects on the stack, mem leak with new? Do you throw exceptions in C++ with or without the new operator? Because both of these would throw an exception. v

What Exceptions can PostAsJsonAsync throw?

Hi i would like to catch possible exceptions that PostAsJsonAsync can throw. I cannot find any exception information on the microsoft website about possible exceptions this method can throw. i would l

How to throw good exceptions?

I heard you should never throw a string because there is a lack of information and you’ll catch exceptions you dont expect to catch. What are good practice for throwing exceptions? do you inherit a ba

.NET Throwing Custom Exceptions

Can anyone shed some light on the pros and cons of throwing custom exceptions (which inherit from System.Exception), or the proper way to use them? I’m already aware of the when/when not to throw exce

Throwing and Catching Exceptions

Hey StackOverflow Community, Regarding throwing exceptions. Generally, when do I throw and exception, and when do I catch it? Let’ say I come across these situation where I have to quit because of som

Throwing exceptions from constructor – is it necessary subclass has to throw as well?

I am reading a topic of throwing exceptions from constructors. After a bit of studying same topic over stackflow. I have come to conclusion that we can throw exceptions from constructors. The issue oc


Throw a class that’s derived from std::exception; if you #include <stdexcept>, you can pick from a number of ready-made, useful derived classes.

Deriving from std::exception allows your handlers to follow a recognizable style, as you can always use .what() to get a textual message. Don’t throw primitive types, since they carry no semantic information.

Generally people don’t throw std::exception directly for the simple reason that it doesn’t store any error messages. There wouldn’t be anything for the what method to return. I get confused sometimes over this because MSVC provides a non-standard extension to this a parameterized constructor in std::exception that accepts a string.

You can choose among existing exception classes like std::runtime_exception or define your own. This is somewhat subjective but I recommend keeping the number of exception classes to a minimum as RAII can eliminate a lot of the need to have multiple code branches and catch blocks for different exception types. Often the message combined with RAII-conforming code is enough to gracefully recover from any exception.

And finally, I recommend all exceptions you throw inherit from std::exception for similar reasons. You don’t want to have to litter your code with many different catch blocks for different exception types if you can avoid it. Solve the problem as generally as you can.

Unlike java you CAN throw whatever (int, string, MyClass, …) you want. But listen to Kerrek. 🙂

Usually you’ll want to throw one of the exceptions derived from std::exception as others have said.

On occasion I’ve thrown other types, but only if it’s caught within the same block and the value is something useful in that context.

The primary exception to throwing something derived from std::exception would be if you’re using some framework (e.g., MFC) with its own exception hierarchy. In that case, you generally want to derive from an appropriate spot in their hierarchy instead.

Note that I’m not particularly attempting to hold MFC up as an example of clean exception handling (or clean design in general), only an example of a framework that includes an exception hierarchy. When you’re using a framework that already defines an exception hierarchy, you’re generally better off using it.

In other words, unlike the preference in C++ otherwise, it’s generally accepted that exceptions should be a single, monolithic hierarchy with a single root. For the standard library, that single root is std::exception, but other frameworks have alternatives, and if they provide one you generally want to fit yours into it.