What should one throw?, C/C++ Programming

A: C++, unlike only about every other language with exceptions, is extremely accomodating while it comes to what you can throw. Actually, you can throw anything you akin to. That begs the question then, what should you throw?

In general, it's best to throw objects, not built-ins. If achievable, you must throw instances of classes which derive (ultimately) from the std::exception class. Through making your exception class inherit (ultimately) from the standard exception base-class, you are making life simpler for users (they have the alternative of catching most things by std::exception), plus probably you are providing them with more information (as the fact that your particular exception might be a refinement of std::runtime_error or whatever).

The common practice is to throw temporary:


class MyException : public std::runtime_error {


MyException() : std::runtime_error("MyException") { }


void f()


// ...

throw MyException();


Here, a temporary of type MyException is created & thrown. Class MyException inherits from class std::runtime_error that (ultimately) inherits from class std::exception.

Posted Date: 3/19/2013 8:55:09 AM | Location : United States

Related Discussions:- What should one throw?, Assignment Help, Ask Question on What should one throw?, Get Answer, Expert's Help, What should one throw? Discussions

Write discussion on What should one throw?
Your posts are moderated
Related Questions
a program to find the area under curve y=f(x) between x=a and x=b,integrate y=f(x) between the limits of a and b.

A Padovan string P(n) for a natural number n is defined as: P(0) = ‘X’ P(1) = ‘Y’ P(2) = ‘Z’ P(n) = P(n-2) + P(n-3), n>2

Program to show the ascii value of characters: int main() {                 int one_char;                 cout                 one_char = getch();

Aim: To implement a program to allocate memory dynamically for 2 dimensional array (accept and print matrix) using pointers. Code:                       #include #inc

Structures A structure is a derived data type. It is a combination of logically related data items. Unlike arrays, which are a collection of such as data types, structures can

What is the difference among malloc/free and new/delete? A: Malloc/free do not know about destructors and constructors. New & delete create and destroy objects, whereas malloc &

Define global variable in c++ program: How to define a global variable and need of global variable in c++ program. int main() {    int m=20;    clrscr();    for

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Sine function computation

how to write c++ code for financial system using class and object