Connecting Tech Pros Worldwide Forums | Help | Site Map

Exception handling -- the significance of "try"

pauldepstein@att.net
Guest
 
Posts: n/a
#1: Sep 10 '06
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


Rolf Magnus
Guest
 
Posts: n/a
#2: Sep 10 '06

re: Exception handling -- the significance of "try"


pauldepstein@att.net wrote:
Quote:
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.

Roy Smith
Guest
 
Posts: n/a
#3: Sep 10 '06

re: Exception handling -- the significance of "try"


In article <ee10qj$cui$01$1@news.t-online.com>,
Rolf Magnus <ramagnus@t-online.dewrote:
Quote:
pauldepstein@att.net wrote:
>
Quote:
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.
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.
Rolf Magnus
Guest
 
Posts: n/a
#4: Sep 10 '06

re: Exception handling -- the significance of "try"


Roy Smith wrote:
Quote:
Quote:
Quote:
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.
>
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().

benben
Guest
 
Posts: n/a
#5: Sep 10 '06

re: Exception handling -- the significance of "try"


pauldepstein@att.net wrote:
Quote:
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
pauldepstein@att.net
Guest
 
Posts: n/a
#6: Sep 10 '06

re: Exception handling -- the significance of "try"



Rolf Magnus wrote:
....
Quote:
>
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

Alf P. Steinbach
Guest
 
Posts: n/a
#7: Sep 10 '06

re: Exception handling -- the significance of "try"


* pauldepstein@att.net:
Quote:
Rolf Magnus wrote:
...
Quote:
>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.

--
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?
pauldepstein@att.net
Guest
 
Posts: n/a
#8: Sep 10 '06

re: Exception handling -- the significance of "try"



Alf P. Steinbach wrote:
....
Quote:
'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

Alf P. Steinbach
Guest
 
Posts: n/a
#9: Sep 10 '06

re: Exception handling -- the significance of "try"


* pauldepstein@att.net:
Quote:
Alf P. Steinbach wrote:
...
Quote:
>'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.
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.

--
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?
Thomas J. Gritzan
Guest
 
Posts: n/a
#10: Sep 10 '06

re: Exception handling -- the significance of "try"


pauldepstein@att.net schrieb:
Quote:
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.
Quote:
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.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Gernot Frisch
Guest
 
Posts: n/a
#11: Sep 11 '06

re: Exception handling -- the significance of "try"


Quote:
>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;
}


Closed Thread