473,473 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Exception handling -- the significance of "try"

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

Sep 10 '06 #1
10 2397
pa**********@att.net wrote:
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.

Sep 10 '06 #2
In article <ee*************@news.t-online.com>,
Rolf Magnus <ra******@t-online.dewrote:
pa**********@att.net wrote:
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.
Sep 10 '06 #3
Roy Smith wrote:
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().

Sep 10 '06 #4
pa**********@att.net wrote:
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
Sep 10 '06 #5

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

Sep 10 '06 #6
* pa**********@att.net:
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.

--
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?
Sep 10 '06 #7

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

Sep 10 '06 #8
* pa**********@att.net:
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.
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?
Sep 10 '06 #9
pa**********@att.net schrieb:
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.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Sep 10 '06 #10
>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;
}
Sep 11 '06 #11

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

Similar topics

1
by: Klaus Neuner | last post by:
Hello, as I am desperately searching for a (straightforward) solution of the following problem, I'll give it another try: How can I tell Python to do things of the following kind: for n...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
6
by: bill salkin | last post by:
I setup a "Try..." block and attempted to open a non- existant database. It went to the second "catch" not the first. What is the proper "catch" clause for this specific case? TIA, Bill
9
by: Jameson.Quinn | last post by:
I have: try: for line in open(myFileName): count += 1 except IOError: print "Can't open myfile" (I know, this is bad, I never close the file, but its just for illustration). But then I...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.