|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| An Exceptional Quest |
|
![]() SteveC Member since: 1/9/2000 From: Boston, USA |
||||
|
|
||||
| Shameless plug... Another article on C++ exceptions: http://www.gamedev.net/reference/programming/features/cppexception/ One note: The use of goto should be safe. Although I frown upon it as do most. Goto's that leave a scope should properly invoke the destructors of objects that are local to that scope. Not doing so is a compiler bug. |
||||
|
||||
![]() Stoffel Member since: 12/2/1999 From: San Diego, USA |
||||
|
|
||||
| Good article. Missing one important thing: If you're using exceptions anyway, and you want to throw an exception class instead of an int or such, why not use the standard exceptions? The "exception" standard library gives you an exception base class that can hold a "what ()" string. "stdexcept" gives you a little hierarchy of exceptions: domain_error, invalid_argument, length_error, logic_error, runtime_error, etc. You can use these directly or derive from them. These are all in namespace std:: and all standard C++. |
||||
|
||||
![]() SteveC Member since: 1/9/2000 From: Boston, USA |
||||
|
|
||||
| Good point about the stdexcept classes. I guess one reason why one might want to use a separate exception class heirarchy is to differentiate to clients which exceptions originated in the standard library and which came from your own code. |
||||
|
||||
![]() Void Member since: 9/12/1999 From: Singapore |
||||
|
|
||||
| > Shameless plug... It was a great read while I was learning about exceptions. I wished you have some concrete examples about the threading and others though. (Hint) >The use of goto should be safe. Although I frown upon it as do most. The bad things about using goto is you have to pre declare all your variables at the top of the function (before the first goto branch). I read somewhere that it is not safe to branch using goto, setjmp or the stack might not be cleaned up properly during unwinding. >If you're using exceptions anyway, and you want to throw an exception class instead of an int or such, why not use the standard exceptions? I throw an int exception as a simple example. If you read on to the next sample, it shows throwing an exception class. In the last section ( I guess it's was too lengthy? ), I did give suggestion to inherit the exception hierarchy from the standard exception. I personally find the standard exception lacking. IMO, It should give a stack trace and provide as much information as possible. Just an error string what() is almost the same as a crash address. (At least I can log the crash and find the function from the pdb) |
||||
|
||||
![]() paulecoyote Member since: 7/14/2003 From: Alton, United Kingdom |
||||
|
|
||||
| I know that this article is a bit old and thread a bit stale and necro'ed, but I was wondering if anyone had an example of an exception inherited from the stl exception - and what advantages doing that would have from just using your own exception class. |
||||
|
||||
![]() DigitalDelusion Member since: 10/4/2000 From: Sodertalje, Sweden |
||||
|
|
||||
Quote: It's all about granularity I commonly inherit from the standard exceptions it gives clients a choice if they desire and need the granularity that my library exceptions offer they have it and if they choose to ignore it they won't get caught in uncaught exception hell as long as they handle standard exceptions correctly. I would actually say it's advicable to *always* inherit from the appropriate stdandar exception and that exceptions to that should be rare. |
||||
|
||||
![]() paulecoyote Member since: 7/14/2003 From: Alton, United Kingdom |
||||
|
|
||||
Quote: yeah I see your point, it's nicer to be able to catch (exception) rather then catch (...) and not have an object to ask what of. Any example implementations of a simple derived class of exception that properly also handles all the contructors, operators and functions that also need to be overridden? |
||||
|
||||
![]() DigitalDelusion Member since: 10/4/2000 From: Sodertalje, Sweden |
||||
|
|
||||
struct ExceptionWhosNameTellMeWhatWentNuts : public std::runtime_error
{
ExceptionWhosNameTellMeWhatWentNuts(): std::runtime_error( "something went b0rked during runtime"){}
};
;or if you're a bit daring:
class ExceptionThatHasSomethingIntrestingToSay : public std::exception
{
public:
ExceptionThatHasSomethingIntrestingToSay(){}
const char *what() const
{
//create a cool error string
return my_cool_error_string;
}
};
you really don't need to worry about it more than that, assignment/copy shouldn't be a problem either as long as you catch your exceptions via reference. |
||||
|
||||
![]() paulecoyote Member since: 7/14/2003 From: Alton, United Kingdom |
||||
|
|
||||
| You don't need to mess with the operators? What if your exception defined more things (like some kind of stack trace) - how would you invoke the base operator to make sure the private stuff in the base exception is copied as well as your stuff? Is it any different then how you would do that for your own hierachy that did not involve the stl? |
||||
|
||||
![]() DigitalDelusion Member since: 10/4/2000 From: Sodertalje, Sweden |
||||
|
|
||||
Quote: You shoulnd't be copying exceptions, they should be catched by (const) reference. But if you do try that you'll probably wander in to slicing issues just as you would for pass by value of any other class. |
||||
|
||||
![]() TiitHns Member since: 1/6/2008 From: Tallinn, Estonia |
||||
|
|
||||
| i wont bother making new thread. I have slight problem [CODE] m_hWnd = ::CreateWindowEx(dwExstyle, CE_CLASS_NAME, TEXT("Wnd"), dwstyle, iWndLeft, iWndTop, iRealWidth, iRealHeight, NULL, NULL, m_hInstance, this); [/CODE] m_hWnd is private member variable of class. This piece of code is at constructor, but when checking it with if(!m_hWnd) it returns exception. but GetLastError() tells me that everything is ok. Can any one point how to solve this problem? |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|