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
Selection sort - C program: Write a program to define a selection sort. void main()  {   clrscr();   int a[100],ch,n;   cout   cin>>n;   for (int i=0;i

#question.Write a c++ program to accept and print a 1_d array

A: Method of passing arguments to a function that takes parameter of type reference.  for instance: void swap( int & x, int & y ) { int temp = x; x =

Define the Data Type Modifiers in C language? There are three data types(int, float & double) above have the following modifiers. 1. short 2. long 3. signed 4. unsigned

I want a PROGRAM - Facebook auto like system to increase fans to different fanpage It has in a program that take Facebook mails and passwords from a excel file (.xlsx) for examp

I need to program a microprocessor that would have a temperature, gas, humidity and PIR sensor connected to it. It would send the data to a PC via a Zigbee and receive commands fro

Program of swapping two varibales: void swap(int *, int *);   // This is swap's prototype int main() {                 int x = 5, y = 7;                 swap(&x, &

Change the matrix program (program 3) slightly. Overload == operator to compare two matrices to be added or subtracted. i.e., whether the column of first and the row of second

I am an online tutor who can teach computer science and programming language. Is there any requirement for this?