Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Throwing a simple exception

Reply
Thread Tools

Throwing a simple exception

 
 
JKop
Guest
Posts: n/a
 
      10-23-2004

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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      10-23-2004

"JKop" <> wrote in message
news:%nxed.39911$...
>
> 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


 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      10-23-2004
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:
>


<exception>

defines std::exception

<stdexecpt>

defines a few general purpose exceptions derived from it.
 
Reply With Quote
 
David Lindauer
Guest
Posts: n/a
 
      10-23-2004


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:
>
> 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
}
}
 
Reply With Quote
 
Computer Whizz
Guest
Posts: n/a
 
      10-24-2004

"David Lindauer" <> wrote in message
news:...
>
>
> 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 <exception> errors too? Or is this just for <stdexcept> ?

--
=========
Comp Whizz
=========
(The C++ beginner)


 
Reply With Quote
 
David Lindauer
Guest
Posts: n/a
 
      10-24-2004


Computer Whizz wrote:

> "David Lindauer" <> wrote in message
> news:...


>
> <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
 
Reply With Quote
 
Computer Whizz
Guest
Posts: n/a
 
      10-27-2004

"David Lindauer" <> wrote in message
news:...
>
>
> Computer Whizz wrote:
>
>> "David Lindauer" <> wrote in message
>> news:...

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


 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      10-27-2004

> 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
 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      10-27-2004
In message <clo4ep$ss7$>, Computer Whizz
<> 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.

--
Richard Herring
 
Reply With Quote
 
David Lindauer
Guest
Posts: n/a
 
      10-28-2004


Richard Herring wrote:

> In message <clo4ep$ss7$>, Computer Whizz
> <> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert.aspx throwing exception =?Utf-8?B?RXVzdGljZSBTY3J1YmI=?= ASP .Net 0 09-23-2005 03:51 AM
System.IO.Directoryinfo throwing exception =?Utf-8?B?R2xlbm4gVmVuemtl?= ASP .Net 3 07-25-2005 09:49 PM
WebRequest.GetResponse() throwing exception (Internal Server error =?Utf-8?B?VGVycnk=?= ASP .Net 4 01-13-2005 10:25 PM
Problem of throwing an exception (System.Net.Sockets.OverlappedAsyncResult::CompletionPortCallback) VincentWong ASP .Net 1 12-29-2003 09:32 AM
Throwing Exception in a composite control KJ ASP .Net 5 07-25-2003 01:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57