Exception handling -- the significance of "try"

P

pauldepstein

I have read about exception handling in a few books and websites but am
still confused on a basic point.

I understand the try ... catch syntax.

However, I have seen examples of using throw where the throw-command is
not in a try-block.

What is the difference in meaning between throwing in a try block and
throwing outside a try block?

Paul Epstein
 
R

Rolf Magnus

I have read about exception handling in a few books and websites but am
still confused on a basic point.

I understand the try ... catch syntax.

However, I have seen examples of using throw where the throw-command is
not in a try-block.

What is the difference in meaning between throwing in a try block and
throwing outside a try block?

There is no difference. Note that you don't need to catch an exception in
the function that threw it. You can also catch it in another function that
calls it.
 
R

Roy Smith

Rolf Magnus said:
There is no difference. Note that you don't need to catch an exception in
the function that threw it. You can also catch it in another function that
calls it.

Or not catch it all, in which case it's caught by some low-level runtime
code that ran before your main() routine was called.
 
R

Rolf Magnus

Roy said:
Or not catch it all, in which case it's caught by some low-level runtime
code that ran before your main() routine was called.

Actually, std::terminate() is called, for which you can define your own
handler. The default handler simply calls std::abort().
 
B

benben

I have read about exception handling in a few books and websites but am
still confused on a basic point.

I understand the try ... catch syntax.

However, I have seen examples of using throw where the throw-command is
not in a try-block.

What is the difference in meaning between throwing in a try block and
throwing outside a try block?

Paul Epstein


Rarely will a throw statement appear in a try block. Like below:


void g()
{
if (error_detected()) // I don't know how to deal with it
throw Error(); // so I just should report it
}

void f()
{
try
{
g(); // handles error if g throws
}
catch (Error e)
{
// the best way to handle it here is to let the user know
cout << e;

// then run the recovery plan
recover_system();
}
}


Why?

Suppose you are a supermarket check out staff. You find out the check
out machine is not adding the princes up. What do you do? You report it
and let someone who knows how to handle it to handle it.

The error report gets passed on through a number of people and finally
ended in the hand of your friend, Fred, who is the supermarket's
technician. Fred kindly does the fixes.

In C++ a throw statement REPORTS an error that the code has no idea how
to appropriately deal with. A try-catch block HANDLES the error.

If the function which throws the an exception actually knows how to
handle it then it shouldn't have throw it in the first place.

Regards,
Ben
 
P

pauldepstein

Rolf Magnus wrote:
....
There is no difference [between throwing in a try block and throwing elsewhere]. Note that you don't need to catch an exception in
the function that threw it. You can also catch it in another function that
calls it.

So "try" has no real syntactical significance? The only purpose of
"try" is as a comment that exceptions-handling code is expected?

Paul Epstein
 
A

Alf P. Steinbach

* (e-mail address removed):
Rolf Magnus wrote:
...
There is no difference [between throwing in a try block and throwing elsewhere]. Note that you don't need to catch an exception in
the function that threw it. You can also catch it in another function that
calls it.

So "try" has no real syntactical significance? The only purpose of
"try" is as a comment that exceptions-handling code is expected?

'try' directs exceptions in the following block to the corresponding set
of 'catch' clauses, where an exception will be caught if it matches.
 
P

pauldepstein

Alf P. Steinbach wrote:
....
'try' directs exceptions in the following block to the corresponding set
of 'catch' clauses, where an exception will be caught if it matches.

So what happens if there is no "try" word attached to the block.
Surely the same catch clauses will be addressed and there will be no
difference in behaviour?

The catch clauses follow the try block so it seems that throwing would
direct exceptions to the corresponding catch clauses without the
keyword 'try'.

I still don't understand what "try" is doing because it seems to me
that the ordinary logic flow of the program, together with the
definition of "throw", would lead to the correct catch clause in any
case.

Paul Epstein
 
A

Alf P. Steinbach

* (e-mail address removed):
Alf P. Steinbach wrote:
...

So what happens if there is no "try" word attached to the block.
Surely the same catch clauses will be addressed and there will be no
difference in behaviour?

The catch clauses follow the try block so it seems that throwing would
direct exceptions to the corresponding catch clauses without the
keyword 'try'.

I still don't understand what "try" is doing because it seems to me
that the ordinary logic flow of the program, together with the
definition of "throw", would lead to the correct catch clause in any
case.

If you omit the 'try' and keep the 'catch' clauses, you get a syntax error.

Yes, it's syntactic sugar: the language syntax could have been designed
without 'try'.

Consult your nearest textbook to learn about C++ syntax.
 
T

Thomas J. Gritzan

So what happens if there is no "try" word attached to the block.

Syntax error. The Syntax is:

try
{
[statements]
}
[one ore catch blocks]

catch-without-try is like else-without-if.
I still don't understand what "try" is doing because it seems to me
that the ordinary logic flow of the program, together with the
definition of "throw", would lead to the correct catch clause in any
case.

Look at this example:

#include <iostream>
int main()
{
throw 1;

try
{
}
catch (...)
{
std::cout << "catch" << std::endl;
}
}

The code in the catch block will not run, because the throw statement is
not in the try block. Exceptions can only be catched, when the code that
has thrown, comes at any enclosing function scope to a try{} block.

An exception _leaves_ the current function almost immediately (after
cleanup), and no further statements will be executed, until a try-block is
reached.

HTH.
 
G

Gernot Frisch

I have read about exception handling in a few books and websites but
am
still confused on a basic point.

I understand the try ... catch syntax.

However, I have seen examples of using throw where the throw-command
is
not in a try-block.

What is the difference in meaning between throwing in a try block
and
throwing outside a try block?

See the try/throw thing serves one great purpose. If you have a
function that calls a function and a function and a f....
and this function detects an error that is of type "can not continue
to work", then this function can "throw" an error and all the
functions before that called this one get dropped. You're at the
place, where you want to handle the error:


void foo(int i)
{
// i>0 -> recursively call foo()
while(i>0) foo(--i);

// i == 0 -> imagine all the "foo" stack above this call
throw(0);
}


int main()
{
try
{
foo(13);
}
catch(...)
{
cout << "there was an error foo'ing 13";
}
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top