469,366 Members | 2,254 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

What happens when an exception is not caught?

Sorry for the rudimentary question. I found the following posting
online and did not know the answer myself.
"Reminds me of a time I had a phone interview concerning a C++ job.
Didn't help that the interviewer had a very thick Slavic accent. He
asked me what happens when an exception doesn't get caught. I told him
the program would terminate. He said that's not the answer he was
looking for. I gave him a kind of detailed description of the throw
process leading to "then it gets to the top level of the program and
if there is no exception handling there, the program terminates." He
said "no, what I was looking for is that the unhandledExceptionHandler
is called" (might not have the correct name.)"
Is there a default exception handler?

Aug 23 '07 #1
10 11836
On 2007-08-23 21:21, Digital Puer wrote:
Sorry for the rudimentary question. I found the following posting
online and did not know the answer myself.
"Reminds me of a time I had a phone interview concerning a C++ job.
Didn't help that the interviewer had a very thick Slavic accent. He
asked me what happens when an exception doesn't get caught. I told him
the program would terminate. He said that's not the answer he was
looking for. I gave him a kind of detailed description of the throw
process leading to "then it gets to the top level of the program and
if there is no exception handling there, the program terminates." He
said "no, what I was looking for is that the unhandledExceptionHandler
is called" (might not have the correct name.)"
Is there a default exception handler?
Yes, the terminate() function is called, the user can specify a function
to be called by terminate(), however this function is not allowed to
return, but must terminate execution of the program. But it is allowed
to do some other stuff first.

--
Erik Wikström
Aug 23 '07 #2
Digital Puer wrote:
Sorry for the rudimentary question. I found the following posting
online and did not know the answer myself.
"Reminds me of a time I had a phone interview concerning a C++ job.
Didn't help that the interviewer had a very thick Slavic accent. He
asked me what happens when an exception doesn't get caught. I told him
the program would terminate. He said that's not the answer he was
looking for. I gave him a kind of detailed description of the throw
process leading to "then it gets to the top level of the program and
if there is no exception handling there, the program terminates." He
said "no, what I was looking for is that the unhandledExceptionHandler
is called" (might not have the correct name.)"
Is there a default exception handler?
The default "unhandled exception handler" is "std::terminate". The
Standard describes the cases in which it's called in [except.terminate]
(15.5.1 in the current edition).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 23 '07 #3
On Thu, 23 Aug 2007 12:21:38 -0700, Digital Puer wrote:
Is there a default exception handler?

or you can make sure that any exception is caught by using
try {}
catch(exception_type_a a) {}
catch(exception_type_b b) {}
catch(exception_type_c c) {}
catch(...) {}

the (...) means catch any uncaught exception that is thrown in the try
block

otherwise, as others have mentioned, the terminate() function can provide
a graceful exit from your program.

Aug 24 '07 #4
Hi!

Erik Wikström schrieb:
Yes, the terminate() function is called, the user can specify a function
to be called by terminate(), however this function is not allowed to
return, but must terminate execution of the program. But it is allowed
to do some other stuff first.
What is the state of global variables in a terminate handler which is
installed by set_terminate? Are they destructed prior to entering the
handler?

Frank
Aug 24 '07 #5
Frank Birbacher wrote:
[..]
What is the state of global variables in a terminate handler which is
installed by set_terminate? Are they destructed prior to entering the
handler?
It's not really specified, but since the default terminate_handler is
'abort' and destructors of static objects are not called priort to
'abort' (if called by the user), they will likely not be called prior
to the call to your terminate_handler, either.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 24 '07 #6
Hi!

Victor Bazarov schrieb:
It's not really specified, but since the default terminate_handler is
'abort' and destructors of static objects are not called priort to
'abort' (if called by the user), they will likely not be called prior
to the call to your terminate_handler, either.
Does then (or should) abort() care for destruction of globals? Or is it
that the destructors are never run in this case?

Frank
Aug 24 '07 #7
Frank Birbacher wrote:
Hi!

Victor Bazarov schrieb:
>It's not really specified, but since the default terminate_handler is
'abort' and destructors of static objects are not called priort to
'abort' (if called by the user), they will likely not be called prior
to the call to your terminate_handler, either.

Does then (or should) abort() care for destruction of globals? Or is
it that the destructors are never run in this case?
It's up to the implementors of the library whether 'abort' should care
for destruction of globals. As to your own 'terminate_handler', it is
up to you, but in most cases you won't be able to do anything except
[attempt] to call 'exit' instead of 'abort', but since your handler is
called in a funky state of the program, I am not sure calling 'exit'
is actually a good idea.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 25 '07 #8
On Aug 24, 7:11 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Frank Birbacher wrote:
Hi!
Victor Bazarov schrieb:
It's not really specified, but since the default terminate_handler is
'abort' and destructors of static objects are not called priort to
'abort' (if called by the user), they will likely not be called prior
to the call to your terminate_handler, either.
Does then (or should) abort() care for destruction of globals? Or is
it that the destructors are never run in this case?

It's up to the implementors of the library whether 'abort' should care
for destruction of globals. As to your own 'terminate_handler', it is
up to you, but in most cases you won't be able to do anything except
[attempt] to call 'exit' instead of 'abort', but since your handler is
called in a funky state of the program, I am not sure calling 'exit'
is actually a good idea.
Incidentally somebody posted a test case explaining this exact
situation at c.l.c++.m. See here:
http://groups.google.com/group/comp....74d65d0a890bae

Aug 25 '07 #9
On Aug 24, 7:11 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Frank Birbacher wrote:
Hi!
Victor Bazarov schrieb:
It's not really specified, but since the default terminate_handler is
'abort' and destructors of static objects are not called priort to
'abort' (if called by the user), they will likely not be called prior
to the call to your terminate_handler, either.
Does then (or should) abort() care for destruction of globals? Or is
it that the destructors are never run in this case?

It's up to the implementors of the library whether 'abort' should care
for destruction of globals. As to your own 'terminate_handler', it is
up to you, but in most cases you won't be able to do anything except
[attempt] to call 'exit' instead of 'abort', but since your handler is
called in a funky state of the program, I am not sure calling 'exit'
is actually a good idea.
Incidentally somebody posted a test case explaining this exact
scenario at c.l.c++.m. See here:
http://groups.google.com/group/comp....74d65d0a890bae

Aug 25 '07 #10
In article <pa*********************@somedomain.com>,
no*********@somedomain.com says...

[ ... ]
otherwise, as others have mentioned, the terminate() function can provide
a graceful exit from your program.
....at least if you consider being dragged out by the heels after being
shot in the head a "graceful exit". :-)

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 26 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by David Mertz | last post: by
12 posts views Thread by Dario | last post: by
24 posts views Thread by Steven T. Hatton | last post: by
10 posts views Thread by linq936 | last post: by
24 posts views Thread by Dave | last post: by
16 posts views Thread by HillBilly | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.