Home » Community » Forums » » An Exceptional Quest
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 An Exceptional Quest
Post Reply 
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.

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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++.

 User Rating: 1100   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

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.

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

> 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)

 User Rating: 1017   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1643   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

Quote:
Original post by paulecoyote
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.


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.

 User Rating: 1513   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by DigitalDelusion
Quote:
Original post by paulecoyote
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.


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.


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?

 User Rating: 1643   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

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.

 User Rating: 1513   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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?

 User Rating: 1643   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

Quote:
Original post by paulecoyote
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?


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.

 User Rating: 1513   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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?

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: