-
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
-
November 28th, 2009, 11:21 AM
#2
Re: Using exceptions in the real world.
Originally Posted by couling
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.
What's undefined about that?
In fact, this is the only way to throw that makes sense to me. Make sure you always catch by const reference.
Also, if you create your own exception classes, it is best to make them exception safe. Else, there is the possibility that an exception is thrown while you are attempting to throw an exception. Not very useful.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
November 28th, 2009, 11:24 AM
#3
Re: Using exceptions in the real world.
I have not seen 'new Exception()' in any c++ program that I've seen. Why would you want to allocate the exception from the heap?
I also find it a bit peculiar that you want to use a global exception instance - I've always seen, and used, what you think seems like undefined behaviour. It just makes a new exception instance and throws that.
This way, you don't have to worry about resetting something like an error message string held in the exception every time it is caught.
Code:
#include <iostream>
int main()
{
int x = 1;
try
{
if(x==1)
throw std::exception("x==1");
}
catch( std::exception & e )
{
std::cout << "err msg: " << e.what() << std::endl;
}
++x;
try
{
if(x==2)
throw std::exception("x==2");
}
catch( std::exception & e )
{
std::cout << "err msg: " << e.what() << std::endl;
}
try
{
if(x==2)
throw std::exception();
}
catch( std::exception & e )
{
std::cout << "err msg: " << e.what() << std::endl;
}
return 0;
}
output:
Code:
err msg: x==1
err msg: x==2
err msg: Unknown exception
-
November 28th, 2009, 12:00 PM
#4
Re: Using exceptions in the real world.
A warning about Amleto's example: std::exception does not have a constructor that takes a string as an argument, so the program should not compile, unless the standard library implementation extended the interface of std::exception to include a constructor that takes a string as an argument. Furthermore, you should directly #include <exception>.
-
November 28th, 2009, 01:01 PM
#5
Re: Using exceptions in the real world.
I've used the first method a fair bit while working with delphi classes as these can't be created on the stack. It never seemd the most elagent solution though.
I only mentioned the second method because that was what was shown on cplusplus.com. I've never seen it used in practice.
As for the third method.
Originally Posted by D_Drmmr
What's undefined about that?
It seemed that way because I'm unshore of how the exceptions model works. The code is creating something on the stack right before it rolls a load off the stack. The more I think about it there's nothing wrong with that as I guess it's modeled vaguely like return.
I'll need to do tests on is how and when the exception object is copied with this method.
Last edited by couling; November 28th, 2009 at 01:05 PM.
Signature
Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
End Signature
-
November 28th, 2009, 10:57 PM
#6
Re: Using exceptions in the real world.
There's an extremely major problem with throwing/catching pointers. As illustrated:
Code:
#include <exception>
#include <ctime>
std::exception globalException;
void foo()
{
throw &globalException;
}
void bar()
{
throw new std::exception();
}
int main()
{
srand(time(0));
try
{
if (rand() % 2)
{
foo();
}
else
{
bar();
}
}
catch (const std::exception* e)
{
// delete e, or not? We have no way to know
}
return 0;
}
In other words, you basically want to avoid throwing/catching pointers unless someone puts a gun to your head and tells you you have no choice.
-
November 29th, 2009, 07:49 AM
#7
Re: Using exceptions in the real world.
Originally Posted by couling
I'll need to do tests on is how and when the exception object is copied with this method.
It's copied when you copy it. As long as you catch by const reference and don't copy an instance of an exception class yourself, nothing gets copied. If you want to be sure, declare the copy c'tor and assignment operator of the exception class as private.
Code:
class MyException : public std::exception
{
public:
MyException();
private:
// disable at compile time
MyException(const MyException&);
MyException& operator =(const MyException&);
};
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
November 29th, 2009, 09:04 AM
#8
Re: Using exceptions in the real world.
Originally Posted by Speedo
There's an extremely major problem with throwing/catching pointers.
The other problem is that operator new can throw an exception itself (unless it is overridden using the nothrow version).
Regards,
Paul McKenzie
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
|