|
-
November 28th, 2009, 08:45 AM
#1
Using exceptions in the real world.
In most of the commertial programming I've done, I rarely have uses for exceptions because of issues with interfacing them with other (non-c++) code. When I have used them I've tended to follow the lead of the library that threw them. But I'm building something for myself where that's no issue and I've realised I'm really fuzzy on how they should be used within c++ in the real world.
Can anyone who uses them regularly in their code advice me of a reccomended method to throw them...
In java the only way to throw an exception is
Code:
throw new MyException();
I'm assuming that in c++ i need to delete after I catch with this version.
Alternatively I see that I can do something like:
Code:
class myexception: public exception
{
} myex;
int main () {
try
{
throw myex;
}
catch (exception& e)
{
}
return 0;
}
// Thanks cplusplus.com
But in a little of the code I use, I have good reason to want to hold two of the same exception at once. Using a static instance wouldn't be my natural choice here.
I've also seen code simular to the previous, but which uses
Code:
throw myexception(); // NOT myex
This seems like undefined behaviour to me, but I'm not sure.
Any clarification on this would be great, especially if you regularly use them.
Regards
Signature
Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
End Signature
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|