Re: catch (...)

From: Cy Edmunds (cedmunds_at_spamless.rochester.rr.com)
Date: 01/14/04


Date: Wed, 14 Jan 2004 03:07:44 GMT


"Tom Groszko" <newscorrespondent@charter.net> wrote in message
news:400457e2_1@Usenet.com...
> Given a try catch scenario
>
> try
> { dosomething();
> }
> catch (...)
> { report some error
> }
>
> Is there any way to figure out what exception is being thrown and
> represented by the ellipsis in the above code?
> I am using MS C++ if that environment has something different.
>
> I am catching an error and sure would like to know what is wrong. I have
> tried to catch everything in the docs for the library and none of those
are
> the error.
>
> Thanks
> Tom G.
[snip]

You could try this:

try
{
    dosomething();
}
catch (std::exception &e)
{
    std::cerr << "std::exception " << typeid(e).name() << '\n';
    std::cerr << e.what() << '\n';
}
catch (...)
{
    std::cerr << "non-standard exception\n";
}

If you don't know where the error is coming from but whoever threw it was
nice enough to base his exception class on std::exception you should be able
to get some information possibly including the actual type name and perhaps
an error message. OK, so that would be lucky. But if you were getting an
exception from MY code it would work. :)

catch(...) should be a last resort because once you are there you never know
what to do.

-- 
Cy
http://home.rochester.rr.com/cyhome/
PS depending on your compiler you might have to do something specific to
turn on RTTI.


Relevant Pages

  • catch (...)
    ... Given a try catch scenario ... {dosomething(); ... {report some error ... Is there any way to figure out what exception is being thrown and ...
    (comp.lang.cpp)
  • Re: Handling exceptions in functions
    ... returns false and raise the exception, ... procedure DoSomething(aParam: Integer); ... DoSomething(aParam2); ... So, for example, expected errors in a program may be ...
    (borland.public.delphi.non-technical)
  • Re: finalize()
    ... public void doSomething() throws Exception { ... File file = null; ... You failed to catch the Exception that doSomethingmight throw. ...
    (comp.lang.java.programmer)
  • Re: Is it possible for a try...finally...end construct to fail?
    ... :) try/finally and try/except are completely different ... side effect of Delphi's global exception handler). ... Probably show an error message to the user at this ...
    (borland.public.delphi.language.objectpascal)
  • Re: preserve exception within try .. finally
    ... The exception raised by doSomething() travels all the way up the call stack ... getResource() # don't free if it cannot be acquired ... I think that the canHandleIttest and the reraising is not very common, ...
    (comp.lang.python)

Loading