Throwing a simple exception

J

JKop

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
 
J

John Harrison

JKop said:
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
 
R

Ron Natalie

JKop said:
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.
 
D

David Lindauer

JKop said:
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
}
}
 
C

Computer Whizz

David Lindauer said:
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 <exception> errors too? Or is this just for <stdexcept> ?
 
D

David Lindauer

Computer said:
<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
 
C

Computer Whizz

David Lindauer said:
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.
 
J

JKop

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
 
R

Richard Herring

Computer Whizz said:
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.
 
D

David Lindauer

Richard said:
An exception is simply something that is thrown and caught. You can
throw pretty much anything.
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
 
C

Computer Whizz

Yes - sorry... I'm thinking back to VB and generally call any bug an "error"
on my part - since it IS.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,230
Latest member
LifeBoostCBD

Latest Threads

Top