Throwing a simple exception

Discussion in 'C++' started by JKop, Oct 23, 2004.

  1. JKop

    JKop Guest

    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
    JKop, Oct 23, 2004
    #1
    1. Advertising

  2. "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
    John Harrison, Oct 23, 2004
    #2
    1. Advertising

  3. JKop

    Ron Natalie Guest

    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.
    Ron Natalie, Oct 23, 2004
    #3
  4. 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
    }
    }
    David Lindauer, Oct 23, 2004
    #4
  5. "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)
    Computer Whizz, Oct 24, 2004
    #5
  6. 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
    David Lindauer, Oct 24, 2004
    #6
  7. "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)
    Computer Whizz, Oct 27, 2004
    #7
  8. JKop

    JKop Guest


    > 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
    JKop, Oct 27, 2004
    #8
  9. 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
    Richard Herring, Oct 27, 2004
    #9
  10. 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
    David Lindauer, Oct 28, 2004
    #10
  11. "David Lindauer" <> wrote in message
    news:...
    >
    >
    > Richard Herring wrote:
    >
    >> In message <clo4ep$ss7$>, Computer Whizz
    >> <> 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.

    >>
    >> >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)
    Computer Whizz, Oct 28, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KJ
    Replies:
    5
    Views:
    376
    John Saunders
    Jul 25, 2003
  2. VincentWong
    Replies:
    1
    Views:
    554
    VincentWong
    Dec 29, 2003
  3. =?Utf-8?B?VGVycnk=?=
    Replies:
    4
    Views:
    5,993
    =?Utf-8?B?VGVycnk=?=
    Jan 13, 2005
  4. =?Utf-8?B?R2xlbm4gVmVuemtl?=

    System.IO.Directoryinfo throwing exception

    =?Utf-8?B?R2xlbm4gVmVuemtl?=, Jul 25, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,695
    John Timney \(ASP.NET MVP\)
    Jul 25, 2005
  5. =?Utf-8?B?RXVzdGljZSBTY3J1YmI=?=

    Insert.aspx throwing exception

    =?Utf-8?B?RXVzdGljZSBTY3J1YmI=?=, Sep 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    574
    =?Utf-8?B?RXVzdGljZSBTY3J1YmI=?=
    Sep 23, 2005
Loading...

Share This Page