473,513 Members | 2,563 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Throw question

If I code as follows

try
{
\\ do something
}
catch (.....Exception)
{
throw;
}

What should happen?
A test program isn't doing what I had expected it would do.
Hence this question.

Adrian.
Nov 29 '06 #1
7 1378
Hi!

If the exception specified in the catch clause is thrown somewhere
inside the try clause, then it will be caught. However, since your catch
branch only has a rethrow statement, the same exception will be thrown
again - if you do not have any additional code in the catch part, it
will look like as if you had no try/catch at all.

What is the behavior you are expecting from this code?

-Lenard

Adrian < wrote:
If I code as follows

try
{
\\ do something
}
catch (.....Exception)
{
throw;
}

What should happen?
A test program isn't doing what I had expected it would do.
Hence this question.

Adrian.

Nov 29 '06 #2
Supposig my sample code is all there is,
Could you add code to make it work?
Thanks.

"Lenard Gunda" <ms**@frenzy.huwrote in message
news:OR**************@TK2MSFTNGP02.phx.gbl...
Hi!

If the exception specified in the catch clause is thrown somewhere
inside the try clause, then it will be caught. However, since your catch
branch only has a rethrow statement, the same exception will be thrown
again - if you do not have any additional code in the catch part, it
will look like as if you had no try/catch at all.

What is the behavior you are expecting from this code?

-Lenard

Adrian < wrote:
If I code as follows

try
{
\\ do something
}
catch (.....Exception)
{
throw;
}

What should happen?
A test program isn't doing what I had expected it would do.
Hence this question.

Adrian.

Nov 29 '06 #3
Hi,

Possibly, but I still don't know how you want it to work? Do you want to
stop the exception from being rethrown, or what is it that you would
like to accomplish?

-Lenard
Adrian < wrote:
Supposig my sample code is all there is,
Could you add code to make it work?
Thanks.

"Lenard Gunda" <ms**@frenzy.huwrote in message
news:OR**************@TK2MSFTNGP02.phx.gbl...
>Hi!

If the exception specified in the catch clause is thrown somewhere
inside the try clause, then it will be caught. However, since your catch
branch only has a rethrow statement, the same exception will be thrown
again - if you do not have any additional code in the catch part, it
will look like as if you had no try/catch at all.

What is the behavior you are expecting from this code?

-Lenard

> Adrian < wrote:
If I code as follows

try
{
\\ do something
}
catch (.....Exception)
{
throw;
}

What should happen?
A test program isn't doing what I had expected it would do.
Hence this question.

Adrian.


Nov 29 '06 #4
Throw in catch block means current exception processing isn't enough or couldn't do it, and then; rethrow the exception object to the outer space, either try...catch pair or runtime/system exception handling mechanism.
"Adrian <" <no*@all.accessibleдÈëÏûÏ¢ news:45********************@dreader2.news.tiscali. nl...
If I code as follows

try
{
\\ do something
}
catch (.....Exception)
{
throw;
}

What should happen?
A test program isn't doing what I had expected it would do.
Hence this question.

Adrian.
Nov 29 '06 #5
On Wed, 29 Nov 2006 12:36:40 +0100, "Adrian <" <no*@all.accessible>
wrote:
>If I code as follows

try
{
\\ do something
}
catch (.....Exception)
{
throw;
}

What should happen?
A test program isn't doing what I had expected it would do.
Hence this question.

Adrian.
That depends on what you expected it to do. Currently your code just
rethrows the original exception, so unless you have another catch
statement somewhere the exception will remain uncaught and the program
will crash, at the point of your "throw", with an uncaught exception.

When I run:

static void Main() {

try {
int x = 20;
int y = 0;
int z = x / y;
}

catch (DivideByZeroException) {
Console.WriteLine("Inside catch.");
throw;
}

Console.Write("Press [Enter] to continue... ");
Console.ReadLine();
}

I get "Inside catch." displayed on the console and then the program
crashes with an uncaught DivideByZero exception, as I would expect it
to. Also, the crash happened at the "throw" and not at the "z = x /
y". The original exception was correctly caught, it is the
re-throwing of the exception that is not caught and causes the actual
crash.

rossum

Nov 29 '06 #6
Thank you. I have no more questions.
It is clear now.
Adrian.
************
"rossum" <ro******@coldmail.comwrote in message
news:9g********************************@4ax.com...
On Wed, 29 Nov 2006 12:36:40 +0100, "Adrian <" <no*@all.accessible>
wrote:
If I code as follows

try
{
\\ do something
}
catch (.....Exception)
{
throw;
}

What should happen?
A test program isn't doing what I had expected it would do.
Hence this question.

Adrian.
That depends on what you expected it to do. Currently your code just
rethrows the original exception, so unless you have another catch
statement somewhere the exception will remain uncaught and the program
will crash, at the point of your "throw", with an uncaught exception.

When I run:

static void Main() {

try {
int x = 20;
int y = 0;
int z = x / y;
}

catch (DivideByZeroException) {
Console.WriteLine("Inside catch.");
throw;
}

Console.Write("Press [Enter] to continue... ");
Console.ReadLine();
}

I get "Inside catch." displayed on the console and then the program
crashes with an uncaught DivideByZero exception, as I would expect it
to. Also, the crash happened at the "throw" and not at the "z = x /
y". The original exception was correctly caught, it is the
re-throwing of the exception that is not caught and causes the actual
crash.

rossum

Nov 29 '06 #7
Thank you.
Adrian.
********
"Lyle Avery" <g.******@gmail.comwrote in message
news:u8****************@TK2MSFTNGP02.phx.gbl...
Throw in catch block means current exception processing isn't enough or
couldn't do it, and then; rethrow the exception object to the outer space,
either try...catch pair or runtime/system exception handling mechanism.
"Adrian <" <no*@all.accessibleD¡ä¨¨????¡é
news:45********************@dreader2.news.tiscali. nl...
If I code as follows

try
{
\\ do something
}
catch (.....Exception)
{
throw;
}

What should happen?
A test program isn't doing what I had expected it would do.
Hence this question.

Adrian.

Nov 29 '06 #8

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

Similar topics

3
1441
by: lixiaoyao | last post by:
when I run the following code,it just generate "aborted",but the anticipated answer is "Caught \"char *\" exception" ,it seems to be no run the 'catch' sentence. please help! #include <iostream>...
0
1140
by: Lasse Vågsæther Karlsen | last post by:
From the book by Jeffrey Richter: throw; will not change the origin of the exception, whereas throw ex; will change the origin of the exception to this statement. When I try the following...
2
3063
by: Kerri | last post by:
Hi, I am new to .NET In my Error Logic on my Aspx pages when an error happens it hits my catch statement where I throw an Exception. My question is : what is the difference between Thwo...
4
14883
by: James Radke | last post by:
Hello, I am attempting to use the proper Try/Catch technique when accessing my Microsoft SQL server database and have a question... If I use something similar to the following: Try set up...
13
2037
by: Jacek Dziedzic | last post by:
Hi! <OT, background> I am in a situation where I use two compilers from different vendors to compile my program. It seems that recently, due to a misconfiguration, library conflict or my...
1
1474
by: Ralph Moritz | last post by:
Hi, I've written a class called ConfigParser, which can be used to parse config files. (Surprise!) I have overloaded the shift operators to mimic the std iostreams. My question is, should...
8
1975
by: Grizlyk | last post by:
Hello I want to understand the exception habdling in C++ more, because i think no one can apply anything free (free - without remembering large unclear list of rules and exceptions from rules...
18
3201
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
6
1944
by: jason.cipriani | last post by:
Consider this program, which defines a template class who's template parameter is the type of an exception that can be thrown by members of the class: === BEGIN EXAMPLE === #include...
0
7259
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
7523
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
5683
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,...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4745
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
3232
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
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
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 ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.