Throw question

  • Thread starter Thread starter > Adrian
  • Start date Start date
A

> Adrian

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.
 
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
 
Supposig my sample code is all there is,
Could you add code to make it work?
Thanks.
 
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
 
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 <" <[email protected]> дÈëÏûÏ¢ 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.
 
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
 
Thank you. I have no more questions.
It is clear now.
Adrian.
************
rossum said:
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
 
Thank you.
Adrian.
********
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 <" <[email protected]> D¡ä¨¨????¡é
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.
 

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

Back
Top