 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
JKop Guest
|
Posted: Sat Oct 23, 2004 6:27 pm Post subject: Throwing a simple exception |
|
|
Let's say you've a very simple function, that, if it fails, should throw an
exception. The thing is though, it's not important enough to go and actually
define an "exception class" for, so... is there any general exception class
defined in the Standard Library that I could use in such circumstances,
maybe something like:
namespace std
{
class exception {};
}
void MyFunc()
{
//Something bad happens
throw std::exception();
}
-JKop
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Sat Oct 23, 2004 6:45 pm Post subject: Re: Throwing a simple exception |
|
|
"JKop" <NULL (AT) NULL (DOT) NULL> wrote
| Quote: |
Let's say you've a very simple function, that, if it fails, should throw
an
exception. The thing is though, it's not important enough to go and
actually
define an "exception class" for, so... is there any general exception
class
defined in the Standard Library that I could use in such circumstances,
maybe something like:
namespace std
{
class exception {};
}
void MyFunc()
{
//Something bad happens
throw std::exception();
}
|
std::exception is just such a class, its defined in the header <exception>.
You could also consider std::runtime_error in <stdexcept>, it derives from
std::exception and allows you to specify a message.
throw std::runtime_error("wobbly wibble");
john
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Sat Oct 23, 2004 7:03 pm Post subject: Re: Throwing a simple exception |
|
|
JKop wrote:
| Quote: | Let's say you've a very simple function, that, if it fails, should throw an
exception. The thing is though, it's not important enough to go and actually
define an "exception class" for, so... is there any general exception class
defined in the Standard Library that I could use in such circumstances,
maybe something like:
|
<exception>
defines std::exception
<stdexecpt>
defines a few general purpose exceptions derived from it.
|
|
| Back to top |
|
 |
David Lindauer Guest
|
Posted: Sat Oct 23, 2004 9:20 pm Post subject: Re: Throwing a simple exception |
|
|
JKop wrote:
| Quote: | Let's say you've a very simple function, that, if it fails, should throw an
exception. The thing is though, it's not important enough to go and actually
define an "exception class" for, so... is there any general exception class
defined in the Standard Library that I could use in such circumstances,
maybe something like:
namespace std
{
class exception {};
}
void MyFunc()
{
//Something bad happens
throw std::exception();
}
-JKop
|
for example:
#include <stdexcept>
using namespace std ;
void myfunc(bool bad_things_happen)
{
if (bad_things_happen)
throw runtime_error("my message here") ; // use a runtime_error since
you can't associate a
//message
with the standard 'exception' class
}
int main(int argc, char **argv)
{
try {
myfunc(true) ;
} catch (runtime_error &aa) { // note you could catch 'exception' here if you
want to catch all the
// exceptions in stdexcept
cout << aa.what() ; // prints your message
}
}
|
|
| Back to top |
|
 |
Computer Whizz Guest
|
Posted: Sun Oct 24, 2004 10:07 am Post subject: Re: Throwing a simple exception |
|
|
"David Lindauer" <camille (AT) bluegrass (DOT) net> wrote
| Quote: |
JKop wrote:
Let's say you've a very simple function, that, if it fails, should throw
an
exception. The thing is though, it's not important enough to go and
actually
define an "exception class" for, so... is there any general exception
class
defined in the Standard Library that I could use in such circumstances,
maybe something like:
for example:
#include <stdexcept
using namespace std ;
void myfunc(bool bad_things_happen)
{
if (bad_things_happen)
throw runtime_error("my message here") ; // use a runtime_error
since
you can't associate a
//message
with the standard 'exception' class
}
int main(int argc, char **argv)
{
try {
myfunc(true) ;
} catch (runtime_error &aa) { // note you could catch 'exception' here
if you
want to catch all the
// exceptions in stdexcept
cout << aa.what() ; // prints your message
}
}
|
Wow - I knew about the exception messages - but didn't know you could catch
them...
Can you catch
--
=========
Comp Whizz
=========
(The C++ beginner)
|
|
| Back to top |
|
 |
David Lindauer Guest
|
Posted: Sun Oct 24, 2004 9:11 pm Post subject: Re: Throwing a simple exception |
|
|
Computer Whizz wrote:
| Quote: | "David Lindauer" <camille (AT) bluegrass (DOT) net> wrote in message
news:417ACB1B.F20CCAA9 (AT) bluegrass (DOT) net...
snip
Wow - I knew about the exception messages - but didn't know you could catch
them...
Can you catch <exception> errors too? Or is this just for <stdexcept> ?
|
you can catch them if you want.... you can really catch anything you want.
catch () is type-based and doesn't really care about specific declarations made
in some header. I think I may have even caught an 'int' in some forgotten
program .
David
|
|
| Back to top |
|
 |
Computer Whizz Guest
|
Posted: Wed Oct 27, 2004 12:27 pm Post subject: Re: Throwing a simple exception |
|
|
"David Lindauer" <camille (AT) bluegrass (DOT) net> wrote
| Quote: |
Computer Whizz wrote:
"David Lindauer" <camille (AT) bluegrass (DOT) net> wrote in message
news:417ACB1B.F20CCAA9 (AT) bluegrass (DOT) net...
snip
Wow - I knew about the exception messages - but didn't know you could
catch
them...
Can you catch <exception> errors too? Or is this just for <stdexcept> ?
you can catch them if you want.... you can really catch anything you
want.
catch () is type-based and doesn't really care about specific declarations
made
in some header. I think I may have even caught an 'int' in some forgotten
program .
David
|
Along these lines - I just read in accel C++ that errors have different
types.
Say you wanted an outer catch that would catch different types of errors for
multiple reasons - I don't know exactly why - maybe just to save me typing
loads of catches around area's that would give out errors... Is there such a
way to do a catch?
As it seems like you can only catch the errors with the appropriate type.
--
=========
Comp Whizz
=========
(The C++ beginner)
|
|
| Back to top |
|
 |
JKop Guest
|
Posted: Wed Oct 27, 2004 12:34 pm Post subject: Re: Throwing a simple exception |
|
|
| Quote: | Say you wanted an outer catch that would catch different types of
errors for multiple reasons - I don't know exactly why - maybe just to
save me typing loads of catches around area's that would give out
errors... Is there such a way to do a catch?
As it seems like you can only catch the errors with the appropriate
type.
|
What the hell is an "outer catch"?
Anyway:
catch(...)
{
}
If you wanted a bullet-proof program:
int main()
{
try
{
//Program goes here
}
catch(...)
{
return 1;
}
}
But still, you've to watch out for global objects whose contructor may
throw...
-JKop
|
|
| Back to top |
|
 |
Richard Herring Guest
|
Posted: Wed Oct 27, 2004 1:09 pm Post subject: Re: Throwing a simple exception |
|
|
In message <clo4ep$ss7$1 (AT) newsg1 (DOT) svr.pol.co.uk>, Computer Whizz
<old486whizz (AT) hotmail (DOT) com> writes
| Quote: | Along these lines - I just read in accel C++ that errors
|
Exceptions.
| Quote: | have different
types.
|
An exception is simply something that is thrown and caught. You can
throw pretty much anything.
| Quote: |
Say you wanted an outer catch that would catch different types of errors for
multiple reasons - I don't know exactly why - maybe just to save me typing
loads of catches around area's that would give out errors... Is there such a
way to do a catch?
As it seems like you can only catch the errors with the appropriate type.
Read about polymorphism and the "is-a" relationship. |
If you derive your different error types from a single type Base, you
can catch them all with "catch (Base const & b)" because each derived
type is-a Base.
--
Richard Herring
|
|
| Back to top |
|
 |
David Lindauer Guest
|
Posted: Thu Oct 28, 2004 1:31 am Post subject: Re: Throwing a simple exception |
|
|
Richard Herring wrote:
| Quote: | In message <clo4ep$ss7$1 (AT) newsg1 (DOT) svr.pol.co.uk>, Computer Whizz
[email]old486whizz (AT) hotmail (DOT) com[/email]> writes
Along these lines - I just read in accel C++ that errors
Exceptions.
have different
types.
An exception is simply something that is thrown and caught. You can
throw pretty much anything.
Say you wanted an outer catch that would catch different types of errors for
multiple reasons - I don't know exactly why - maybe just to save me typing
loads of catches around area's that would give out errors... Is there such a
way to do a catch?
As it seems like you can only catch the errors with the appropriate type.
Read about polymorphism and the "is-a" relationship.
If you derive your different error types from a single type Base, you
can catch them all with "catch (Base const & b)" because each derived
type is-a Base.
|
along those lines you can catch anything in <stdexcept> with catch( exception
const &b) because everything in that header is derived from exception...
David
|
|
| Back to top |
|
 |
Computer Whizz Guest
|
Posted: Thu Oct 28, 2004 7:36 am Post subject: Re: Throwing a simple exception |
|
|
"David Lindauer" <camille (AT) bluegrass (DOT) net> wrote
| Quote: |
Richard Herring wrote:
In message <clo4ep$ss7$1 (AT) newsg1 (DOT) svr.pol.co.uk>, Computer Whizz
[email]old486whizz (AT) hotmail (DOT) com[/email]> writes
Along these lines - I just read in accel C++ that errors
Exceptions.
|
Yes - sorry... I'm thinking back to VB and generally call any bug an "error"
on my part - since it IS.
| Quote: |
have different
types.
An exception is simply something that is thrown and caught. You can
throw pretty much anything.
Say you wanted an outer catch that would catch different types of errors
for
multiple reasons - I don't know exactly why - maybe just to save me
typing
loads of catches around area's that would give out errors... Is there
such a
way to do a catch?
As it seems like you can only catch the errors with the appropriate
type.
Read about polymorphism and the "is-a" relationship.
If you derive your different error types from a single type Base, you
can catch them all with "catch (Base const & b)" because each derived
type is-a Base.
along those lines you can catch anything in <stdexcept> with catch(
exception
const &b) because everything in that header is derived from exception...
David
|
Ah. Thank you very much.
Will look into all this later on.
--
=========
Comp Whizz
=========
(The C++ beginner)
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|