handling exceptions

Discussion in 'C++' started by annex, Jun 5, 2004.

  1. annex

    annex Guest

    hi, i have some queries about handling exceptions, i'm using Borland c++
    Builder 6.

    1) what's the best way to catch multiple exceptions in one catch statement?
    is this possible? for e.g i want to catch 2 exceptions; MyEx1 and MyEx2,
    both to be handled in the same way. how to do this?

    try
    {
    doSomething();
    }
    catch (MyEx1)
    {
    sameCode();
    }
    catch (MyEx2)
    {
    sameCode();
    }

    note that both exceptions are to be handled using the same piece of code.

    2) what's the difference btw catch(...) and catch(Exception). will
    catch(Exception) catch all unhandled exceptions also like does catch(...) ?

    try
    {
    doSomething();
    }
    catch(...)
    {
    handle();
    }

    and

    try
    {
    doSomething();
    }
    catch(Exception)
    {
    handle();
    }

    --------------
    thanks ;-)
    annex, Jun 5, 2004
    #1
    1. Advertising

  2. annex

    Siemel Naran Guest

    "annex" <> wrote in message
    news:...

    > 1) what's the best way to catch multiple exceptions in one catch

    statement?
    > is this possible? for e.g i want to catch 2 exceptions; MyEx1 and MyEx2,
    > both to be handled in the same way. how to do this?
    >
    > try
    > {
    > doSomething();
    > }
    > catch (MyEx1)
    > {
    > sameCode();
    > }
    > catch (MyEx2)
    > {
    > sameCode();
    > }
    >
    > note that both exceptions are to be handled using the same piece of code.


    See if you can derive MyEx1 and MyEx2 from MyEx. Then catch a MyEx,
    preferrably by reference. With this one catch statement you catch all
    exceptions derived from MyEx, like MyEx1 and MyEx2. If catching by
    reference, feel free to call virtual functions of the caught MyEx object,
    and also to throw the exception with "throw;".

    There is no syntax

    catch (MyEx2 || MyEx2)

    The use of goto seems ugly, and I'm not sure if is even allowed.


    > 2) what's the difference btw catch(...) and catch(Exception). will
    > catch(Exception) catch all unhandled exceptions also like does catch(...)

    ?

    catch (Exception) or catch(const Exception&) catch exceptions of type
    Exception or derived from Exception. You can name the exception object as
    in catch(const Exception& e) and then call virtual functions on the object,
    such as e.what(). You can rethrow the exception object.

    catch (...) catches all exceptions. You can't call virtual functions on the
    object. About all you can say is "unhandled exception" and optionally
    rethrow the exception object.
    Siemel Naran, Jun 5, 2004
    #2
    1. Advertising

  3. annex

    Rolf Magnus Guest

    annex wrote:

    > hi, i have some queries about handling exceptions, i'm using Borland
    > c++ Builder 6.
    >
    > 1) what's the best way to catch multiple exceptions in one catch
    > statement? is this possible? for e.g i want to catch 2 exceptions;
    > MyEx1 and MyEx2, both to be handled in the same way. how to do this?
    >
    > try
    > {
    > doSomething();
    > }
    > catch (MyEx1)
    > {
    > sameCode();
    > }
    > catch (MyEx2)
    > {
    > sameCode();
    > }
    >
    > note that both exceptions are to be handled using the same piece of
    > code.


    The only way to do that is by deriving MyEx1 and MyEx2 from a common
    base class and catching this base class.

    > 2) what's the difference btw catch(...) and catch(Exception).


    The former catches all exceptions, but you don't get the exception
    object. The latter catches only objects of type 'Exception'.
    Btw, you should always catch by reference.

    > will catch(Exception) catch all unhandled exceptions also like does
    > catch(...) ?


    No.

    >
    > try
    > {
    > doSomething();
    > }
    > catch(...)
    > {
    > handle();
    > }
    >
    > and
    >
    > try
    > {
    > doSomething();
    > }
    > catch(Exception)
    > {
    > handle();
    > }
    >
    > --------------
    > thanks ;-)
    Rolf Magnus, Jun 5, 2004
    #3
  4. * Rolf Magnus:
    > annex wrote:
    >
    > > hi, i have some queries about handling exceptions, i'm using Borland
    > > c++ Builder 6.
    > >
    > > 1) what's the best way to catch multiple exceptions in one catch
    > > statement? is this possible? for e.g i want to catch 2 exceptions;
    > > MyEx1 and MyEx2, both to be handled in the same way. how to do this?
    > >
    > > try
    > > {
    > > doSomething();
    > > }
    > > catch (MyEx1)
    > > {
    > > sameCode();
    > > }
    > > catch (MyEx2)
    > > {
    > > sameCode();
    > > }
    > >
    > > note that both exceptions are to be handled using the same piece of
    > > code.

    >
    > The only way to do that is by deriving MyEx1 and MyEx2 from a common
    > base class and catching this base class.


    Or you can use 'catch(...)'.


    > > 2) what's the difference btw catch(...) and catch(Exception).

    >
    > The former catches all exceptions, but you don't get the exception
    > object.


    You do. To retrieve the passed-in exception object rethrow and catch
    it. To rethrow it just use 'throw;'.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jun 5, 2004
    #4
  5. annex wrote:


    > 2) what's the difference btw catch(...) and catch(Exception). will
    > catch(Exception) catch all unhandled exceptions also like does catch(...)


    The first will catch all exceptions derived from Exception the later will
    catch all exceptions no matter wether they are derived or not from
    Exception.

    The best way to catch exceptions is to use its hierarchi:

    class exception1 : pubic exception
    {
    };

    class exception2 : public exception1
    {
    };

    class exception3 : public exception2
    {
    };

    try {
    // ........ code here ....
    } catch (exception3 &e3) { // this will catch e3
    // .....
    } catch (exception2 &e2) { // this will catch e2 and e3... supposing the
    //previous catch was not there
    // ......
    } catch (exception1 &e1) { // this will catch e1, e2, e3... as before
    // ......
    } catch (exception &e) { // this will catch e, e1, e2, e3.. as before
    // ......
    } catch (...) { // this will catch everything
    // ...
    }


    Regards

    Roberto
    Roberto =?ISO-8859-15?Q?D=EDaz?=, Jun 5, 2004
    #5
  6. annex

    Rolf Magnus Guest

    Alf P. Steinbach wrote:

    > * Rolf Magnus:
    >> annex wrote:
    >>
    >> > hi, i have some queries about handling exceptions, i'm using
    >> > Borland c++ Builder 6.
    >> >
    >> > 1) what's the best way to catch multiple exceptions in one catch
    >> > statement? is this possible? for e.g i want to catch 2 exceptions;
    >> > MyEx1 and MyEx2, both to be handled in the same way. how to do
    >> > this?


    [example snipped]

    >> The only way to do that is by deriving MyEx1 and MyEx2 from a common
    >> base class and catching this base class.

    >
    > Or you can use 'catch(...)'.


    Of course, but then you will not only catch MyEx1 and MyEx2, but also
    every other exception, and you can't access the exception object. The
    OP didn't make clear whether that would matter or not.

    >> > 2) what's the difference btw catch(...) and catch(Exception).

    >>
    >> The former catches all exceptions, but you don't get the exception
    >> object.

    >
    > You do. To retrieve the passed-in exception object rethrow and catch
    > it. To rethrow it just use 'throw;'.


    I don't see what that would be good for. If I want to catch class
    Exception, then I do so. Why should I first catch(...), rethrow and
    then catch it again by class name?
    Rolf Magnus, Jun 5, 2004
    #6
  7. * Rolf Magnus:
    > Alf P. Steinbach wrote:
    >
    > > * Rolf Magnus:
    > >> annex wrote:
    > >>
    > >> > hi, i have some queries about handling exceptions, i'm using
    > >> > Borland c++ Builder 6.
    > >> >
    > >> > 1) what's the best way to catch multiple exceptions in one catch
    > >> > statement? is this possible? for e.g i want to catch 2 exceptions;
    > >> > MyEx1 and MyEx2, both to be handled in the same way. how to do
    > >> > this?

    >
    > [example snipped]
    >
    > >> The only way to do that is by deriving MyEx1 and MyEx2 from a common
    > >> base class and catching this base class.

    > >
    > > Or you can use 'catch(...)'.

    >
    > Of course, but then you will not only catch MyEx1 and MyEx2, but also
    > every other exception


    Yes, and so?


    > and you can't access the exception object.


    That is incorrect.


    > >>
    > >> The former catches all exceptions, but you don't get the exception
    > >> object.

    > >
    > > You do. To retrieve the passed-in exception object rethrow and catch
    > > it. To rethrow it just use 'throw;'.

    >
    > I don't see what that would be good for. If I want to catch class
    > Exception, then I do so. Why should I first catch(...), rethrow and
    > then catch it again by class name?


    To avoid catching a number of different types a number of different
    places, which just might be the OP's actual problem.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jun 5, 2004
    #7
  8. annex

    Rolf Magnus Guest

    Alf P. Steinbach wrote:

    >> >> The only way to do that is by deriving MyEx1 and MyEx2 from a
    >> >> common base class and catching this base class.
    >> >
    >> > Or you can use 'catch(...)'.

    >>
    >> Of course, but then you will not only catch MyEx1 and MyEx2, but also
    >> every other exception

    >
    > Yes, and so?


    The OP asked how to catch multiple exceptions, not how to catch all of
    them.

    >> and you can't access the exception object.

    >
    > That is incorrect.


    Then how would you access it within the catch(...)?

    >> >> The former catches all exceptions, but you don't get the exception
    >> >> object.
    >> >
    >> > You do. To retrieve the passed-in exception object rethrow and
    >> > catch
    >> > it. To rethrow it just use 'throw;'.

    >>
    >> I don't see what that would be good for. If I want to catch class
    >> Exception, then I do so. Why should I first catch(...), rethrow and
    >> then catch it again by class name?

    >
    > To avoid catching a number of different types a number of different
    > places, which just might be the OP's actual problem.


    I still don't see what that would be good for. Could you provide an
    example?
    Rolf Magnus, Jun 5, 2004
    #8
  9. Rolf Magnus wrote in news:c9solc$njg$00$-online.com in
    comp.lang.c++:

    >> To avoid catching a number of different types a number of different
    >> places, which just might be the OP's actual problem.

    >
    > I still don't see what that would be good for. Could you provide an


    Its good for avoiding goto (ducks:).

    > example?
    >


    /* should only be called within a catch block
    */
    void myhandler()
    {
    try
    {
    throw;
    }
    catch ( MyEx1 const & )
    {
    }
    catch ( MyEx2 const & )
    {
    }
    //handle both cases ...
    }


    void f()
    {
    try
    {
    // something that throws
    }
    catch ( ... )
    {
    myhandler();
    }
    }

    I've never used it, or seen it used (except in NG posts).

    You can of course wrap myhandler() up in another "handler"
    function that handles different exceptions etc etc.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Jun 5, 2004
    #9
  10. annex

    annex Guest

    "Rolf Magnus" <> wrote in message
    news:c9s254$hr9$02$-online.com...
    > annex wrote:
    >


    [snipped]

    >
    > > 2) what's the difference btw catch(...) and catch(Exception).

    >
    > The former catches all exceptions, but you don't get the exception
    > object. The latter catches only objects of type 'Exception'.
    > Btw, you should always catch by reference.
    >
    > > will catch(Exception) catch all unhandled exceptions also like does
    > > catch(...) ?

    >
    > No.
    >


    Why not? aren't all exceptions derived from the base class Exception (in
    BCB)? if so, then won't catching (Exception& e) catch all exceptions same as
    catching (...) ? the only difference is by catching (Exception& e) we can
    access the exception object directly.

    Cheers.
    annex, Jun 6, 2004
    #10
  11. On Sun, 06 Jun 2004 11:36:58 +0800, annex wrote:

    >
    > Rolf Magnus wrote:
    >> annex wrote:
    >>> will catch(Exception) catch all unhandled exceptions also like does
    >>> catch(...) ?

    >>
    >> No.

    >
    > Why not? aren't all exceptions derived from the base class Exception (in
    > BCB)? if so, then won't catching (Exception& e) catch all exceptions same as
    > catching (...) ? the only difference is by catching (Exception& e) we can
    > access the exception object directly.


    All exceptions in the C++ standard are inherited from std::exception, but
    there is no guarantee that library writers will derive their exceptions
    from anything at all. It is perfectly legitimate to throw an intrinsic
    type (int, char *, etc.), which isn't an object at all.

    --
    Some say the Wired doesn't have political borders like the real world,
    but there are far too many nonsense-spouting anarchists or idiots who
    think that pranks are a revolution.
    Owen Jacobson, Jun 7, 2004
    #11
  12. annex

    Rolf Magnus Guest

    annex wrote:

    >> > will catch(Exception) catch all unhandled exceptions also like does
    >> > catch(...) ?

    >>
    >> No.
    >>

    >
    > Why not? aren't all exceptions derived from the base class Exception
    > (in BCB)?


    I have no idea about BCB (isn't this some Borland compiler?). Anyway,
    the standard C++ exceptions are derived from std::exception and nothing
    else. Actually, I thought that by "Exception", you meant your own base
    class for exceptions. You can derive your own exceptions from whatever
    you want or you can even throw an int. Those are all not caught if you
    only catch Exception. So if Exception is the base class for some
    Borland specific exceptions, you will only catch those ones and no
    others.
    Rolf Magnus, Jun 7, 2004
    #12
  13. annex

    Siemel Naran Guest

    "Rolf Magnus" <> wrote in message
    news:ca1ccg$q2s$00$-

    > I have no idea about BCB (isn't this some Borland compiler?). Anyway,
    > the standard C++ exceptions are derived from std::exception and nothing
    > else. Actually, I thought that by "Exception", you meant your own base
    > class for exceptions. You can derive your own exceptions from whatever
    > you want or you can even throw an int. Those are all not caught if you
    > only catch Exception. So if Exception is the base class for some
    > Borland specific exceptions, you will only catch those ones and no
    > others.


    The Borland Exception class is not derived from std::exception in any way,
    only from TObject. But it's a good case for multiple inheritance as
    std::exception is abstract (as what() const = 0 is pure virtual). They
    should derive Exception from TObject and std::exception.

    Anyway, it basically means to catch all exceptions you should to

    catch (Sysutils::Exception& e) { }
    catch (std::exception& e) { }
    catch (...) { /*unknown exception */ }

    If deriving your own generic exception base class from Sysutils::Exception,
    consider using multiple inheritance to derive from both Sysutils::Exception
    and std::exception if you want to be able to treat your exceptions as either
    Borland or standard exceptions. Of course, instead of std::exception, you
    could derive from std::runtime_error or the others.
    Siemel Naran, Jun 7, 2004
    #13
  14. annex

    Xiaobin Yang Guest

    > >> > will catch(Exception) catch all unhandled exceptions also like does
    > >> > catch(...) ?
    > >>
    > >> No.
    > >>

    > >
    > > Why not? aren't all exceptions derived from the base class Exception
    > > (in BCB)?


    you can define your own exception class MyException and throw it out. In
    this case, MyException is not derived from Exception.

    class MyException {};
    class MyClass {
    public:
    void Function() {
    ....
    throw MyException();
    }

    int main(){

    try {
    MyClass o; o.Function();
    }
    catch(Exception) { cout << "Exception\n"; }
    catch(MyException) {cout << "MyException\n";}

    }
    Xiaobin Yang, Jun 7, 2004
    #14
  15. annex

    annex Guest

    > you can define your own exception class MyException and throw it out. In
    > this case, MyException is not derived from Exception.
    >
    > class MyException {};
    > class MyClass {
    > public:
    > void Function() {
    > ....


    This is interesting, but if i were to define my own exception class not
    deriving from any base exception class, should the new exception class carry
    certain properties or functions that will enable it to be a valid exception
    class? if yes, what are the prerequisites for a class to be a valid
    exception class e.g. Message property perhaps?
    annex, Jun 8, 2004
    #15
  16. annex

    annex Guest

    >
    > All exceptions in the C++ standard are inherited from std::exception, but
    > there is no guarantee that library writers will derive their exceptions
    > from anything at all. It is perfectly legitimate to throw an intrinsic
    > type (int, char *, etc.), which isn't an object at all.
    >


    yeah i've read this somewhere but didn't quite understand. when, where and
    why would we want to throw an intrinsic type exception e.g int? pls give a
    simple example how to use it. cheers.
    annex, Jun 8, 2004
    #16
  17. annex

    Tim Orbaker Guest

    annex wrote:
    >>you can define your own exception class MyException and throw it out. In
    >>this case, MyException is not derived from Exception.
    >>
    >>class MyException {};
    >>class MyClass {
    >>public:
    >>void Function() {
    >> ....

    >
    >
    > This is interesting, but if i were to define my own exception class not
    > deriving from any base exception class, should the new exception class carry
    > certain properties or functions that will enable it to be a valid exception
    > class? if yes, what are the prerequisites for a class to be a valid
    > exception class e.g. Message property perhaps?
    >
    >


    In C++ you can throw any value. It can be a constant number, a POD type
    or a class instance. C++ imposes very few restrictions on exceptions.

    What makes an exception valid is a matter of your application. Your
    design choices will determine what is useful and appropriate.

    Personally, I prefer to use exceptions with: the bare minimum necessary
    to generate a meaningful error message; and as much cleanup code as
    possible (it makes for cleaner catch blocks and less duplication of code).

    Tim
    Tim Orbaker, Jun 8, 2004
    #17
  18. * annex:
    >
    > what are the prerequisites for a class to be a valid exception class


    It must have a working and public copy constructor and destructor.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jun 8, 2004
    #18
  19. annex wrote:

    > yeah i've read this somewhere but didn't quite understand. when, where and
    > why would we want to throw an intrinsic type exception e.g int? pls give a
    > simple example how to use it. cheers.


    When doing a very small test program.

    --
    Salu2
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jun 8, 2004
    #19
  20. annex

    red floyd Guest

    annex wrote:
    >>All exceptions in the C++ standard are inherited from std::exception, but
    >>there is no guarantee that library writers will derive their exceptions
    >>from anything at all. It is perfectly legitimate to throw an intrinsic
    >>type (int, char *, etc.), which isn't an object at all.
    >>

    >
    >
    > yeah i've read this somewhere but didn't quite understand. when, where and
    > why would we want to throw an intrinsic type exception e.g int? pls give a
    > simple example how to use it. cheers.
    >


    Quick and dirty test code.

    #include <iostream>
    void throws_an_int()
    {
    throw 1;
    }

    int main(int, char *[])
    {
    try
    {
    throws_an_int();
    std::cout << "no throw!" << std::endl;
    }
    catch (int x)
    {
    std::cout << "caught int exception value " << x << std::endl;
    }
    return 0;
    }
    red floyd, Jun 8, 2004
    #20
    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. Raterus
    Replies:
    6
    Views:
    573
    Raterus
    Aug 24, 2004
  2. Ahmed Moustafa
    Replies:
    5
    Views:
    30,012
    Chris Smith
    Jul 14, 2004
  3. Paul Miller
    Replies:
    3
    Views:
    1,014
    Alex Martelli
    Nov 12, 2003
  4. Replies:
    3
    Views:
    607
    Sherm Pendley
    Apr 16, 2007
  5. Lie
    Replies:
    3
    Views:
    612
Loading...

Share This Page