[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Getting info about an unknown exception

Adem

10/12/2008 7:51:00 PM

Is it possible to get some info about an unknown exception,
ie. the "catch (...)" case below:

catch (const blah1 &ex)
{
cout << "blah1 exception." << endl;
}

catch (const blah2 &ex)
{
cout << "blah2 exception." << endl;
}

catch (...)
{
cout << "unknown exception." << endl;
}

Can one get for example the line number where the
unknown exception was thrown?

9 Answers

Kai-Uwe Bux

10/12/2008 8:05:00 PM

0

Adem wrote:

> Is it possible to get some info about an unknown exception,
> ie. the "catch (...)" case below:
>
> catch (const blah1 &ex)
> {
> cout << "blah1 exception." << endl;
> }
>
> catch (const blah2 &ex)
> {
> cout << "blah2 exception." << endl;
> }
>
> catch (...)
> {
> cout << "unknown exception." << endl;
> }
>
> Can one get for example the line number where the
> unknown exception was thrown?

No.

a) All information used at the point of the catch has to be stored in the
thrown object (simply because that is the only one not subject to stack
unwinding). And, for all you know, what is caught in the (...) catch clause
could be a char.

b) If you find yourself wishing for that option, chances are that your
design needs fixing. What is the underlying problem you are trying to
solve?


Best

Kai-Uwe Bux

Adem

10/12/2008 8:28:00 PM

0

"Kai-Uwe Bux" <jkherciueh@gmx.net> wrote:
> Adem wrote:
>
> > Is it possible to get some info about an unknown exception,
> > ie. the "catch (...)" case below:
> >
> > catch (const blah1 &ex)
> > {
> > cout << "blah1 exception." << endl;
> > }
> >
> > catch (const blah2 &ex)
> > {
> > cout << "blah2 exception." << endl;
> > }
> >
> > catch (...)
> > {
> > cout << "unknown exception." << endl;
> > }
> >
> > Can one get for example the line number where the
> > unknown exception was thrown?
>
> No.
>
> a) All information used at the point of the catch has to be stored in the
> thrown object (simply because that is the only one not subject to stack
> unwinding). And, for all you know, what is caught in the (...) catch clause
> could be a char.
>
> b) If you find yourself wishing for that option, chances are that your
> design needs fixing. What is the underlying problem you are trying to
> solve?

I compiled a huge project of someone else.
The program's main() ends like that given above.
Too bad, since it is a huge project it seems not easy
to find the location of that error. :-(

Kai-Uwe Bux

10/12/2008 8:47:00 PM

0

Adem wrote:

> "Kai-Uwe Bux" <jkherciueh@gmx.net> wrote:
>> Adem wrote:
>>
>> > Is it possible to get some info about an unknown exception,
>> > ie. the "catch (...)" case below:
>> >
>> > catch (const blah1 &ex)
>> > {
>> > cout << "blah1 exception." << endl;
>> > }
>> >
>> > catch (const blah2 &ex)
>> > {
>> > cout << "blah2 exception." << endl;
>> > }
>> >
>> > catch (...)
>> > {
>> > cout << "unknown exception." << endl;
>> > }
>> >
>> > Can one get for example the line number where the
>> > unknown exception was thrown?
>>
>> No.
>>
>> a) All information used at the point of the catch has to be stored in the
>> thrown object (simply because that is the only one not subject to stack
>> unwinding). And, for all you know, what is caught in the (...) catch
>> clause could be a char.
>>
>> b) If you find yourself wishing for that option, chances are that your
>> design needs fixing. What is the underlying problem you are trying to
>> solve?
>
> I compiled a huge project of someone else.
> The program's main() ends like that given above.
> Too bad, since it is a huge project it seems not easy
> to find the location of that error. :-(

Running that thing in a debugger might help. Most can give you a stack
trace. That way, you can find the location where the throw happens.


Best

Kai-Uwe Bux

Salt_Peter

10/12/2008 8:58:00 PM

0

On Oct 12, 3:50 pm, "Adem" <for-usenet...@alicewho.com> wrote:
> Is it possible to get some info about an unknown exception,
> ie. the "catch (...)" case below:
>
> catch (const blah1 &ex)
> {
> cout << "blah1 exception." << endl;
> }
>
> catch (const blah2 &ex)
> {
> cout << "blah2 exception." << endl;
> }
>
> catch (...)
> {
> cout << "unknown exception." << endl;
> }
>
> Can one get for example the line number where the
> unknown exception was thrown?

No, unless you equip the program to do so (add relevent catch clauses
to capture other specific exceptions). In the case where standardized
exceptions come into play, at least add a catch block for
std::exception. If coders stuck to using the existing hierarchy, life
would be much simpler, ie:

#include <iostream>
#include <stdexcept>

// std::runtime_error is a derivative of std::exception
class SomeException
: public std::runtime_error
{
public:
SomeException( const char* ps )
: std::runtime_error( ps ) { }
};

int main()
{
try
{
throw SomeException("error in main()");
}
catch( const std::exception& e )
{
std::cout << e.what() << std::endl;
}
catch( ... )
{
std::cout << "unknown exception\n";
}
}

/*
error in main()
*/

Bart van Ingen Schenau

10/13/2008 8:28:00 AM

0

On Oct 12, 10:46 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> Adem wrote:
>
> > I compiled a huge project of someone else.
> > The program's main() ends like that given above.
> > Too bad, since it is a huge project it seems not easy
> > to find the location of that error. :-(
>
> Running that thing in a debugger might help. Most can give you a stack
> trace. That way, you can find the location where the throw happens.

Commenting the catch(...) clause (temporarily) out may give the
debugger and you an easier time in locating the throw point.
If an exception is not caught at all, the debugger may kick-in
immediately when the throw-expressing is to be executed.
If there is a matching catch-clause, the debugger can only kick-in
when it hits a manually set breakpoint, which you can only set
correctly if you already know where the exception might be coming
from.

>
> Best
>
> Kai-Uwe Bux

Bart v Ingen Schenau

James Kanze

10/13/2008 9:45:00 AM

0

On Oct 12, 10:04 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> Adem wrote:
> > Is it possible to get some info about an unknown exception,
> > ie. the "catch (...)" case below:

[...]
> No.

> a) All information used at the point of the catch has to be
> stored in the thrown object (simply because that is the only
> one not subject to stack unwinding). And, for all you know,
> what is caught in the (...) catch clause could be a char.

> b) If you find yourself wishing for that option, chances are
> that your design needs fixing. What is the underlying problem
> you are trying to solve?

Most likely, he's dealing with a poorly documented third party
library. Which is throwing exceptions without documenting the
fact.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

red floyd

10/13/2008 2:10:00 PM

0

James Kanze wrote:
> On Oct 12, 10:04 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
>> Adem wrote:
>>> Is it possible to get some info about an unknown exception,
>>> ie. the "catch (...)" case below:
>
> [...]
>> No.
>
>> a) All information used at the point of the catch has to be
>> stored in the thrown object (simply because that is the only
>> one not subject to stack unwinding). And, for all you know,
>> what is caught in the (...) catch clause could be a char.
>
>> b) If you find yourself wishing for that option, chances are
>> that your design needs fixing. What is the underlying problem
>> you are trying to solve?
>
> Most likely, he's dealing with a poorly documented third party
> library. Which is throwing exceptions without documenting the
> fact.
>

Or, he could be dealing with Windows, which throws (...) for system errors.

Paavo Helde

10/15/2008 6:24:00 PM

0

"Adem" <for-usenet-5c@alicewho.com> kirjutas:
>> > catch (const blah2 &ex)
>> > {
>> > cout << "blah2 exception." << endl;
>> > }
>> >
>> > catch (...)
>> > {
>> > cout << "unknown exception." << endl;
>> > }
>> >
[...]
> I compiled a huge project of someone else.
> The program's main() ends like that given above.
> Too bad, since it is a huge project it seems not easy
> to find the location of that error. :-(

This is more a debugging problem and thus depending on the environment.
If this is on Windows, then access violations and other thingies can end
up in catch(...). You can use the set_se_translator() function to
intercept and translate those, if needed.

Alternatively, each decent debugger should be able to break the program
execution in the spot when any exception is thrown, although the feature
might be well hidden ;-) E.g. for gdb you need the command: b __cxa_throw

Regards
Paavo

Stephen Horne

10/16/2008 2:58:00 AM

0

On Sun, 12 Oct 2008 21:50:53 +0200, "Adem"
<for-usenet-5c@alicewho.com> wrote:

>Can one get for example the line number where the
>unknown exception was thrown?

Here's one creative approach...

1. Make damn sure you have a safe unmodified copy of the original
source. Visual differencing tools are also likely to come in
handy.

On Windows, I'd just make a temporary subversion repository and
commit the original sources, using TortoiseSVN. That makes it easy
to diff any changes I make in the working copy.

2. Search all files, and replace every throw with some other code,
such as printing the value of __LINE__ then exiting. Anything
along those lines - the details will depend on the platform and
the application.

3. Run, check the exception that gets thrown, and restore each
exception throw when you're sure it's not the problem (exceptions
are not always errors, or at least they may already be handled
correctly).

You're far better off with a debugger, but this kind of approach can
help at times.


BTW - commenting out your catch(...) blocks may also help when
debugging. It converts a caught exception into an uncaught one, which
the debugger is more likely to break at by default.