Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 3rd, 2007, 4:04 PM   #1
l2u
Newbie
 
Join Date: Mar 2007
Posts: 24
Rep Power: 0 l2u is on a distinguished road
exception handling

Hello..

I have some questions about exception handling in c++..

I know its the best to throw object when exception occurs and I wonder what is the best way to do this.
I was thinking about deriving class from std::exception..

Something like:

class myexception : public std::exception {
public:
//...//
};

I wonder:
Would it be smart to do it this way? How do you guys do it?
Would it be smart to have members for storing last_error and other information?

Maybe it would be good to throw it this way (with description):
throw myexception("function() - this is the problem");

Next question:
Show I have more classes to handle different sorts of exceptions?
For instance exception that would make program exit, etc..

Thanks a lot for answers
l2u is offline   Reply With Quote
Old Mar 3rd, 2007, 4:55 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You might want to review the article referenced in my sig (Grumpy on C++ exceptions).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 3rd, 2007, 5:47 PM   #3
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5 grumpy is on a distinguished road
That little article referred to by Dawei covers broader questions than l2u's.

std::exception supplies a virtual function of the form;
   char *what() const;
that classes derived from it must override. That technique is sufficient if the only information about an error that needs to be returned to the caller is simple (eg the type of the exception thrown and a string describing the exception).

Generally, I'm an advocate of having a number of small and simple exception classes (one for each class of error) which allows it to be caught and handled. At most, I may pass some simple data back (eg some error codes) as data members. In practice, I rarely create classes derived from std::exception.

It is never a good idea for an exception class (whether derived from std::exception or not) to rely on dynamic memory allocation (which means an exception class can not have a member which is an std::string or any other standard container). The reason is that a failure during creation of the exception (or copying, as is required during stack unwinding) can cause confusion (eg caller receiving a different exception from that intended).

I never write an exception to "make a program exit". If I want a program to exit, I call exit() or abort(). Exceptions are an error reporting mechanism, not a program termination mechanism. If I throw an exception, it means I expect that a caller has some (albeit, often small in practice) chance of recovering from the error and the error is severe enough that it shouldn't be ignored. If the caller neglects to handle an exception, then the caller (or more accurately the programmer who wrote the caller) accepts that a consequence is program termination.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception Handling with Threads Harakim Software Design and Algorithms 13 Aug 8th, 2006 6:00 AM
Could some please explain classes to me... TCStyle C++ 10 Feb 20th, 2006 3:51 PM
AhhH!!! I can't find anything on this exception... stakeknife ASP 2 Sep 26th, 2005 7:48 AM
Ranting uman C++ 27 Jun 17th, 2005 12:02 PM
exception handling chrome_knob Java 2 May 19th, 2005 5:45 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:30 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC