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
What is a hash function? Hash function: This is the method from the set 'K' of keys into the set 'L' of memory addresses.   H: K → L These are used to verify the address

Problem: (a) Using a class hierarchy of your own choosing, with at least TWO subclass levels, show (i) the use of abstract and concrete forms of Java class and method in you

given integer ''n'' and permutation of numbers 1,2,..n-1, c program to print permutation that lexicograghically precedes given input permutation if permutation is least print input

#C Why don''t online compilers support the header file? What can I do to resolve this? PS. I have tried , does''nt work either. Thannk in advance.

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec


Project Description: I would like to be building regarding a complex mobile application build as we require the signing of an NDA. My firm in based in Montreal Canada and we are

What happens if you write following code? string& foo()

Write a C program to input three real numbers and  print them out as follows : The first variable is ....... and the second one is ...... The last variable is .......... T

Program is to define a class as teacher: Program is to define a class as teacher and collect information about them by using classes and object class teacher   {   pr