Skip to main content
added 398 characters in body
Source Link
Christophe
  • 82.3k
  • 11
  • 136
  • 202

The C++ core guidelinesC++ core guidelines are the first place to look for. And there’s a whole chapter on error handling.

In short, the preferred way in modern C++ is exception handling with try catch, unless your code needs to be interoperable with C or if the error“error” is not an exceptional situation but a very common one, or for a couple of other very special cases.

Now what to throwthrow? It depends what you want to catchcatch! Throwing strings is for short demo codeis for short demo code. Usually, you’d throw some subclasssome subclass of std::exception, or your own exception class preferably your own exception classes: this allows more selective catch blocsselective catch blocs. A quick look at CEI’s secure coding standard is also very useful.

Finally, you need to be aware that a lot of posts out there on exception management and performance are outdated and rely on drawbacks of earlier exception handling in the nineties. This SO question and the selected answer document this very well.

The C++ core guidelines are the first place to look for. And there’s a whole chapter on error handling.

In short, the preferred way in modern C++ is exception handling with try catch, unless your code needs to be interoperable with C or if the error is not an exceptional situation but a very common one, or for a couple of other very special cases.

Now what to throw? It depends what you want to catch! Throwing strings is for short demo code. Usually, you’d throw some subclass of std::exception, or your own exception class: this allows more selective catch blocs. A quick look at CEI’s secure coding standard is also very useful.

Finally, you need to be aware that a lot of posts out there on exception management and performance are outdated and rely on drawbacks of earlier exception handling in the nineties. This SO question and the selected answer document this very well.

The C++ core guidelines are the first place to look for. And there’s a whole chapter on error handling.

In short, the preferred way in modern C++ is exception handling with try catch, unless your code needs to be interoperable with C or if the “error” is not an exceptional situation but a very common one, or for a couple of other very special cases.

Now what to throw? It depends what you want to catch! Throwing strings is for short demo code. Usually, you’d throw some subclass of std::exception, preferably your own exception classes: this allows more selective catch blocs. A quick look at CEI’s secure coding standard is also very useful.

Finally, you need to be aware that a lot of posts out there on exception management and performance are outdated and rely on drawbacks of earlier exception handling in the nineties. This SO question and the selected answer document this very well.

added 408 characters in body
Source Link
Christophe
  • 82.3k
  • 11
  • 136
  • 202

The C++ core guidelines are the first place to look for. And there’s a whole chapter on error handling.

In short, the preferred way in modern C++modern C++ is exception handling with try catch, unless your code needs to be interoperable with C or if the error is not an exceptional situation but a very common one, or for a couple of other very special cases.

Now what to throw? It depends what you want to catch! Throwing strings is for short demo code. Usually, you’d throw some subclass of std::exception, or your own exception class: this allows more selective catch blocs. A quick look at CEI’s secure coding standard is also very useful.

Finally, you need to be aware that a lot of posts out there on exception management and performance are outdated and rely on drawbacks of earlier exception handling in the nineties. This SO question and the selected answer document this very well.

The C++ core guidelines are the first place to look for. And there’s a whole chapter on error handling.

In short, the preferred way in modern C++ is exception handling with try catch, unless your code needs to be interoperable with C or if the error is not an exceptional situation but a very common one, or for a couple of other very special cases.

Now what to throw? It depends what you want to catch! Throwing strings is for short demo code. Usually, you’d throw some subclass of std::exception, or your own exception class: this allows more selective catch blocs.

Finally, you need to be aware that a lot of posts out there on exception management and performance are outdated and rely on drawbacks of earlier exception handling in the nineties.

The C++ core guidelines are the first place to look for. And there’s a whole chapter on error handling.

In short, the preferred way in modern C++ is exception handling with try catch, unless your code needs to be interoperable with C or if the error is not an exceptional situation but a very common one, or for a couple of other very special cases.

Now what to throw? It depends what you want to catch! Throwing strings is for short demo code. Usually, you’d throw some subclass of std::exception, or your own exception class: this allows more selective catch blocs. A quick look at CEI’s secure coding standard is also very useful.

Finally, you need to be aware that a lot of posts out there on exception management and performance are outdated and rely on drawbacks of earlier exception handling in the nineties. This SO question and the selected answer document this very well.

Source Link
Christophe
  • 82.3k
  • 11
  • 136
  • 202

The C++ core guidelines are the first place to look for. And there’s a whole chapter on error handling.

In short, the preferred way in modern C++ is exception handling with try catch, unless your code needs to be interoperable with C or if the error is not an exceptional situation but a very common one, or for a couple of other very special cases.

Now what to throw? It depends what you want to catch! Throwing strings is for short demo code. Usually, you’d throw some subclass of std::exception, or your own exception class: this allows more selective catch blocs.

Finally, you need to be aware that a lot of posts out there on exception management and performance are outdated and rely on drawbacks of earlier exception handling in the nineties.