473,383 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

catch (...)

Is there any way of retrievieng error information (say, from a 'global'
or system wide) error object - when you are in a catch all statement block?

Sometimes it cannot be helped, when something quirky happens and errors
are propagated up the stack - all the way to the 'one size fits all'
catch (...) statement. It would be useful if there is a way of finding
what srewy thing happened and led us to that point.

Apr 21 '06 #1
13 2676
Bit byte wrote:
Is there any way of retrievieng error information (say, from a 'global'
or system wide) error object - when you are in a catch all statement block?

Sometimes it cannot be helped, when something quirky happens and errors
are propagated up the stack - all the way to the 'one size fits all'
catch (...) statement. It would be useful if there is a way of finding
what srewy thing happened and led us to that point.


Sure, if that global error object was properly set before the exception
was thrown (cf. errno). You won't get any information out of the
exception itself (e.g., it's type or a message from
std::exception::what()) since there is no specific exception caught in
a catch-all.

<OT>Some platforms (notably Microsoft) throw "exceptions" for other
errors such as access violations resulting from a wayward pointer.
(Microsoft calls this structure exception handling (SEH). See
http://www.boost.org/more/error_handling.html on how to mitigate SEH's
interference with the debugger.) If that's what you're referring to,
you should ask in a newsgroup for your platform.</OT>

Cheers! --M

Apr 21 '06 #2
* mlimber:
Bit byte wrote:
Is there any way of retrievieng error information (say, from a 'global'
or system wide) error object - when you are in a catch all statement block?

Sometimes it cannot be helped, when something quirky happens and errors
are propagated up the stack - all the way to the 'one size fits all'
catch (...) statement. It would be useful if there is a way of finding
what srewy thing happened and led us to that point.


Sure, if that global error object was properly set before the exception
was thrown (cf. errno). You won't get any information out of the
exception itself (e.g., it's type or a message from
std::exception::what()) since there is no specific exception caught in
a catch-all.


On the contrary, there is a specific exception caught in a catch-all.

Try

#include <cstddef>
#include <stdexcept>
#include <iostream>
#include <ostream>

int main()
{
try
{
throw std::runtime_error( "Indeed" );
}
catch( ... )
{
try
{
throw;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
}
catch( ... )
{
std::cerr << "!Unknown exception" << std::endl;
}
return EXIT_FAILURE;
}
}

Modulo typos, of course; also, won't work with older compilers like VC6.

The inner try-catch can be moved to a separate function.

--
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?
Apr 21 '06 #3
Bit byte wrote:
Is there any way of retrievieng error information (say, from a 'global'
or system wide) error object - when you are in a catch all statement
block?


No.

As Alf pointed out, you can still find a way to access the thing thrown - if
you know its type. In which case, this would suffice:

try { allMyProgram(); }
catch( std::exception & e )
{
cout << whatever;
}
catch(...)
{
cout << whatever;
}

So now the question is how to minimize the impact of an unhandled exception
on your customers. Your program should write a log file that optionally
records each program activity. When the program faults take its log file
and write an acceptance test that produces a similar log file.

As you near the problem, use a debugger with an [off-topic] feature to
breakpoint each 'throw' statement, to find out which one it was.

If you use MS Windows, check with their newsgroups to learn how to catch the
special structured exceptions their OS throws. These are different, but
catch(...) can catch them.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 21 '06 #4
Bit byte wrote:
Is there any way of retrievieng error information (say, from a 'global'
or system wide) error object - when you are in a catch all statement
block?


No.

As Alf pointed out, you can still find a way to access the thing thrown - if
you know its type. In which case, this would suffice:

try { allMyProgram(); }
catch( std::exception & e )
{
cout << whatever;
}
catch(...)
{
cout << whatever;
}

So now the question is how to minimize the impact of an unhandled exception
on your customers. Your program should write a log file that optionally
records each program activity. When the program crashes take its log file
and write an acceptance test that produces a similar log file.

As you near the problem, use a debugger with an [off-topic] feature to
breakpoint each 'throw' statement, to find out which one it was.

If you use MS Windows, check with their newsgroups to learn how to catch the
special structured exceptions their OS throws. These are different, but
catch(...) can catch them.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 21 '06 #5
Alf P. Steinbach wrote:
* mlimber:
Bit byte wrote:
Is there any way of retrievieng error information (say, from a 'global'
or system wide) error object - when you are in a catch all statement
block?

Sometimes it cannot be helped, when something quirky happens and errors
are propagated up the stack - all the way to the 'one size fits all'
catch (...) statement. It would be useful if there is a way of finding
what srewy thing happened and led us to that point.
Sure, if that global error object was properly set before the exception
was thrown (cf. errno). You won't get any information out of the
exception itself (e.g., it's type or a message from
std::exception::what()) since there is no specific exception caught in
a catch-all.


On the contrary, there is a specific exception caught in a catch-all.

Try

#include <cstddef>
#include <stdexcept>
#include <iostream>
#include <ostream>

int main()
{
try
{
throw std::runtime_error( "Indeed" );
}
catch( ... )
{
try
{
throw;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
}
catch( ... )
{
std::cerr << "!Unknown exception" << std::endl;
}
return EXIT_FAILURE;
}
}


Inspiring! But I guess the reason why the OP only catches the exception
in (...) in the first place, is that the exception thrown wasn't even an
std::exception.

Modulo typos, of course; also, won't work with older compilers like VC6.

The inner try-catch can be moved to a separate function.


Regards,
Ben
Apr 22 '06 #6

"Alf P. Steinbach" <al***@start.no> wrote in message
news:4a************@individual.net...
* mlimber:
Bit byte wrote:
Is there any way of retrievieng error information (say, from a 'global'
or system wide) error object - when you are in a catch all statement
block?

Sometimes it cannot be helped, when something quirky happens and errors
are propagated up the stack - all the way to the 'one size fits all'
catch (...) statement. It would be useful if there is a way of finding
what srewy thing happened and led us to that point.
Sure, if that global error object was properly set before the exception
was thrown (cf. errno). You won't get any information out of the
exception itself (e.g., it's type or a message from
std::exception::what()) since there is no specific exception caught in
a catch-all.


On the contrary, there is a specific exception caught in a catch-all.

Try

#include <cstddef>
#include <stdexcept>
#include <iostream>
#include <ostream>

int main()
{
try
{
throw std::runtime_error( "Indeed" );
}
catch( ... )
{
try
{
throw;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
}
catch( ... )
{
std::cerr << "!Unknown exception" << std::endl;
}
return EXIT_FAILURE;
}
}

Modulo typos, of course; also, won't work with older compilers like VC6.

The inner try-catch can be moved to a separate function.


Right! This tip really helped me with a project I'm working on. Thanks!

Cy

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

Apr 25 '06 #7
Cy Edmunds wrote:
On the contrary, there is a specific exception caught in a catch-all.

Try

#include <cstddef>
#include <stdexcept>
#include <iostream>
#include <ostream>

int main()
{
try
{
throw std::runtime_error( "Indeed" );
}
catch( ... )
{
try
{
throw;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
}
catch( ... )
{
std::cerr << "!Unknown exception" << std::endl;
}
return EXIT_FAILURE;
}
}

Modulo typos, of course; also, won't work with older compilers like VC6.

The inner try-catch can be moved to a separate function.


Right! This tip really helped me with a project I'm working on. Thanks!


That tip is a super-complex way to simply say this:

try
{
throw std::runtime_error( "Indeed" );
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
return EXIT_FAILURE;
}
catch( ... )
{
std::cerr << "!Unknown exception" << std::endl;
return EXIT_FAILURE;
}

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 25 '06 #8
* Phlip:
Cy Edmunds wrote:
On the contrary, there is a specific exception caught in a catch-all.

Try

#include <cstddef>
#include <stdexcept>
#include <iostream>
#include <ostream>

int main()
{
try
{
throw std::runtime_error( "Indeed" );
}
catch( ... )
{
try
{
throw;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
}
catch( ... )
{
std::cerr << "!Unknown exception" << std::endl;
}
return EXIT_FAILURE;
}
}

Modulo typos, of course; also, won't work with older compilers like VC6.

The inner try-catch can be moved to a separate function.

Right! This tip really helped me with a project I'm working on. Thanks!


That tip is a super-complex way to simply say this:

try
{
throw std::runtime_error( "Indeed" );
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
return EXIT_FAILURE;
}
catch( ... )
{
std::cerr << "!Unknown exception" << std::endl;
return EXIT_FAILURE;
}


No.

Mostly, the technique is applicable when you have several different
exception types originating from various third-part libraries.

Placing a multiple try-catch like the above in every function really
isn't an option, and using macros to generate such beasties isn't a very
clean solution. A separate exception conversion function is then a
relatively clean way to deal with the exceptions. All it requires is a
catch-all in each function that deals directly with lib functions.

--
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?
Apr 25 '06 #9
Alf P. Steinbach wrote:
* Phlip:
Cy Edmunds wrote:
On the contrary, there is a specific exception caught in a catch-all.

Try

#include <cstddef>
#include <stdexcept>
#include <iostream>
#include <ostream>

int main()
{
try
{
throw std::runtime_error( "Indeed" );
}
catch( ... )
{
try
{
throw;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
}
catch( ... )
{
std::cerr << "!Unknown exception" << std::endl;
}
return EXIT_FAILURE;
}
}

Modulo typos, of course; also, won't work with older compilers like
VC6.

The inner try-catch can be moved to a separate function.
Right! This tip really helped me with a project I'm working on. Thanks!
That tip is a super-complex way to simply say this:

try
{
throw std::runtime_error( "Indeed" );
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
return EXIT_FAILURE;
}
catch( ... )
{
std::cerr << "!Unknown exception" << std::endl;
return EXIT_FAILURE;
}


No.


As written, yes. The place where we wrote "throw std::runtime_error..."
could have been the rest of the program. Either one top-level handler, or
the other, will catch things by type, and catch everything else without its
type.
Mostly, the technique is applicable when you have several different
exception types originating from various third-part libraries.

Placing a multiple try-catch like the above in every function really
isn't an option,
Nobody said anything about putting the handlers in every function.
and using macros to generate such beasties isn't a very
clean solution.
Nobody said we would abuse macros.
A separate exception conversion function is then a
relatively clean way to deal with the exceptions. All it requires is a
catch-all in each function that deals directly with lib functions.


It's now possible you mean this:

catch(...) {
caught();
}

void caught()
{
try
{
throw;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
}
catch( ... )
{
std::cerr << "!Unknown exception" << std::endl;
}

}

Now we can re-use caught() all over the place. Props.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 25 '06 #10
Alf P. Steinbach wrote:
* Phlip:
Cy Edmunds wrote:
On the contrary, there is a specific exception caught in a catch-all.

Try

#include <cstddef>
#include <stdexcept>
#include <iostream>
#include <ostream>

int main()
{
try
{
throw std::runtime_error( "Indeed" );
}
catch( ... )
{
try
{
throw;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
}
catch( ... )
{
std::cerr << "!Unknown exception" << std::endl;
}
return EXIT_FAILURE;
}
}

Modulo typos, of course; also, won't work with older compilers like VC6.

The inner try-catch can be moved to a separate function.
Right! This tip really helped me with a project I'm working on. Thanks!


That tip is a super-complex way to simply say this:

try
{
throw std::runtime_error( "Indeed" );
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
return EXIT_FAILURE;
}
catch( ... )
{
std::cerr << "!Unknown exception" << std::endl;
return EXIT_FAILURE;
}


No.

Mostly, the technique is applicable when you have several different
exception types originating from various third-part libraries.

Placing a multiple try-catch like the above in every function really
isn't an option, and using macros to generate such beasties isn't a very
clean solution. A separate exception conversion function is then a
relatively clean way to deal with the exceptions. All it requires is a
catch-all in each function that deals directly with lib functions.


I agree that using a conversion function such as this is cleaner in
some cases, but logically there is no difference between yours and
Phlip's code -- they both can catch the desired exception types and
give up on others. In the catch-all block of your handler, you still
don't have any information on the "other" exception (except that it is
not one of the explicitly caught types), which is presumably the same
situation the OP is in to begin with.

Cheers! --M

Apr 25 '06 #11
* mlimber:
Alf P. Steinbach wrote:
* Phlip:
Cy Edmunds wrote:

> On the contrary, there is a specific exception caught in a catch-all.
>
> Try
>
> #include <cstddef>
> #include <stdexcept>
> #include <iostream>
> #include <ostream>
>
> int main()
> {
> try
> {
> throw std::runtime_error( "Indeed" );
> }
> catch( ... )
> {
> try
> {
> throw;
> }
> catch( std::exception const& x )
> {
> std::cerr << "!" << x.what() << std::endl;
> }
> catch( ... )
> {
> std::cerr << "!Unknown exception" << std::endl;
> }
> return EXIT_FAILURE;
> }
> }
>
> Modulo typos, of course; also, won't work with older compilers like VC6.
>
> The inner try-catch can be moved to a separate function.
Right! This tip really helped me with a project I'm working on. Thanks!
That tip is a super-complex way to simply say this:

try
{
throw std::runtime_error( "Indeed" );
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
return EXIT_FAILURE;
}
catch( ... )
{
std::cerr << "!Unknown exception" << std::endl;
return EXIT_FAILURE;
} No.

Mostly, the technique is applicable when you have several different
exception types originating from various third-part libraries.

Placing a multiple try-catch like the above in every function really
isn't an option, and using macros to generate such beasties isn't a very
clean solution. A separate exception conversion function is then a
relatively clean way to deal with the exceptions. All it requires is a
catch-all in each function that deals directly with lib functions.


I agree that using a conversion function such as this is cleaner in
some cases, but logically there is no difference between yours and
Phlip's code -- they both can catch the desired exception types and
give up on others.


Feel free to use assembly language.

Or manually inline expanded code.

After all any C++ program can be expressed (a bit more verbosely) in
assembly language, and actually assembly language is a bit more powerful
in what can be done -- it's the logical choice! :-o

In the catch-all block of your handler, you still
don't have any information on the "other" exception (except that it is
not one of the explicitly caught types), which is presumably the same
situation the OP is in to begin with.


Only the OP can clarify whether this technique is useful to him or her.

This wasn't written in response to the OP.

--
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?
Apr 25 '06 #12
Alf P. Steinbach wrote:
* mlimber:
Alf P. Steinbach wrote:
* Phlip:
Cy Edmunds wrote:

>> On the contrary, there is a specific exception caught in a catch-all.
>>
>> Try
>>
>> #include <cstddef>
>> #include <stdexcept>
>> #include <iostream>
>> #include <ostream>
>>
>> int main()
>> {
>> try
>> {
>> throw std::runtime_error( "Indeed" );
>> }
>> catch( ... )
>> {
>> try
>> {
>> throw;
>> }
>> catch( std::exception const& x )
>> {
>> std::cerr << "!" << x.what() << std::endl;
>> }
>> catch( ... )
>> {
>> std::cerr << "!Unknown exception" << std::endl;
>> }
>> return EXIT_FAILURE;
>> }
>> }
>>
>> Modulo typos, of course; also, won't work with older compilers like VC6.
>>
>> The inner try-catch can be moved to a separate function.
> Right! This tip really helped me with a project I'm working on. Thanks!
That tip is a super-complex way to simply say this:

try
{
throw std::runtime_error( "Indeed" );
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
return EXIT_FAILURE;
}
catch( ... )
{
std::cerr << "!Unknown exception" << std::endl;
return EXIT_FAILURE;
}
No.

Mostly, the technique is applicable when you have several different
exception types originating from various third-part libraries.

Placing a multiple try-catch like the above in every function really
isn't an option, and using macros to generate such beasties isn't a very
clean solution. A separate exception conversion function is then a
relatively clean way to deal with the exceptions. All it requires is a
catch-all in each function that deals directly with lib functions.


I agree that using a conversion function such as this is cleaner in
some cases, but logically there is no difference between yours and
Phlip's code -- they both can catch the desired exception types and
give up on others.


Feel free to use assembly language.

Or manually inline expanded code.

After all any C++ program can be expressed (a bit more verbosely) in
assembly language, and actually assembly language is a bit more powerful
in what can be done -- it's the logical choice! :-o


I agree whole-heartedly that the efficiency issues here are not to be
ignored, but my point remains unchallenged: the two code snippets have
the same *logical* effect and by using your (admittedly nifty) trick,
the programmer doesn't gain any information that s/he couldn't have
gotten by adding the same handlers before the catch-all.
In the catch-all block of your handler, you still
don't have any information on the "other" exception (except that it is
not one of the explicitly caught types), which is presumably the same
situation the OP is in to begin with.


Only the OP can clarify whether this technique is useful to him or her.


The problem as I read it was that the OP wanted to get some information
from an unknown exception that was caught. Aside from adding specific
handlers (whether before the catch-all or in a separate function),
there is no way to do this.

Cheers! --M

Apr 25 '06 #13
mlimber wrote:
I agree whole-heartedly that the efficiency issues here are not to be
ignored, but my point remains unchallenged: the two code snippets have
the same *logical* effect and by using your (admittedly nifty) trick,
the programmer doesn't gain any information that s/he couldn't have
gotten by adding the same handlers before the catch-all.
I have no idea what Alf's complaining about, but my version (with caught())
showed how only Alf's code can put the catchers inside a reusable function.
This is a nice technique, iff you have too many exceptions (!!), and iff
you handle them in too many places (!!).
The problem as I read it was that the OP wanted to get some information
from an unknown exception that was caught. Aside from adding specific
handlers (whether before the catch-all or in a separate function),
there is no way to do this.


Hence my original post, to write a coherent log file and such general
robustness techniques...

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Apr 25 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
11
by: kaeli | last post by:
Hey all, I'd like to start using the try/catch construct in some scripts. Older browsers don't support this. What's the best way to test for support for this construct so it doesn't kill...
4
by: Abhishek Srivastava | last post by:
Hello All, I have seen code snippets like try { ..... } catch {
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
23
by: pigeonrandle | last post by:
Hi, Does this bit of code represent complete overkill?! try { //create a treenode TreeNode tn = new TreeNode(); //add it to a treeview tv.Nodes.Add(tn);
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.