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:

#include

class MyException : public std::runtime_error {

public:

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 palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome


Call by Reference Passing variables(parameters) to a function in C can be done in two ways - pass by value, also called as call by value and pass by address or also known as ca

How to write C program (interest calculator) the simple interest on a loan is calculated by the formula interest=principal*rate*days/365; ?

Linear search in array - C program: Write a program in c to define a linear search in array. void main()                 {                 clrscr();

what is virtual datatype



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 #include float start_point,

Produce data from fixed OCR areas scanned. Project Description: Project is to design software which can use character recognition only on certain areas of scanned puff's/ jpe