Reputation: 295
Is it required to throw an exception explicitly in the try block? What happens if I don't throw an exception in the try block?
try
{
// code that throws an exception
throw new IndexOutofRangeException;
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine(ex.Message);
}
EDIT: Specifically, imagine the case there is code in the try block that generates an exception but try
block does not have throw
statement. What will happen?
Upvotes: 0
Views: 1534
Reputation: 415735
What happens if I don't throw an exception in the try block?
If you don't throw exception, and no other exception is raised, the catch
block won't run. The code will instead skip past the catch block and continue executing anything that comes afterwards (including the finally
block, if there is one).
imagine the case there is code in the try block that generates an exception but try block does not have throw statement.
The catch block will still run... if and only if the type of exception matches the type declared by the catch
block. Let's look at several examples. First up is this sample:
try
{
double x = 1 / 0.0;
}
catch (DivideByZeroException ex)
{
Console.WriteLine(ex.Message);
}
In this case, we have code that causes a DivideByZeroException
exception. Since this matches the type used in the catch
block, the exception handler will run.
Next up:
try
{
double x = 1 / 0.0;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
We still have a DivideByZeroException
exception... but since DivideByZeroException
inherited from the base Exception
type, things are still compatible and the catch block will run.
Another one:
try
{
double x = 1 / 0.0;
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
}
We still have a DivideByZeroException
exception, but this time the catch
block is not compatible. In this case, the program will crash because of the unhandled exception (unless this exception is handled somewhere else in the program stack).
One more:
try
{
double x = 1 / 0.0;
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Two catch
blocks! The first block will not run, because it is not compatible with the DivideByZeroException
type. However, the second catch
block will run, because it is compatible. The exception is handled, and the program can continue.
Finally:
double DbyZ(double numerator)
{
try
{
return numerator / 0.0;
}
catch(InvalidOperationException ex)
{
Console.WriteLine("Invalid operation");
}
}
...
try
{
DbyZ(1.0);
}
catch(Exception ex)
{
Console.WriteLine("Exception");
}
This time, the output will be simply "Exception"
. Remember, you don't always have to catch exceptions in the same place where they're thrown.
If you still have questions about what might happen in a give case, create your own samples to test the case and see.
Upvotes: 2
Reputation: 283634
If code creates an exception but does not throw it, try
/catch
doesn't see it. Throwing an exception is the only thing that makes an exception different from any other class type.
.NET exceptions are software artifacts. Although many of them correspond to hardware traps or OS errors, the hardware does not produce, generate, or throw .NET exception objects. What happens is that the other errors are being detected by code in the .NET runtime engine that then creates and throws a .NET exception object, translating the lower-level error information into properties on the .NET exception.
That translation/wrapping code may be invoked in an event-driven fashion, for example by installing a handler for Windows Structured Exception Handling, rather than having code to test every arithmetic result for floating-point NaNs. But it still is software. As a specific example, in Joel's answer, the code 1 / 0.0
does not generate a DivideByZeroException
. It generates an FPU exception, and the common language runtime handles that FPU exception. It is the handler in the CLR that creates and throws the DivideByZeroException
instance which lands in your catch
block.
Upvotes: -1
Reputation: 11
OP:
throw new IndexOutofRangeException;
This would not even compile(no brackets).
You should throw exceptions if something is unexpected in your domain and you should catch them in the place where you know how to handle them.
You wrote:
"There is code in the try block that generates an exception but try block does not have throw statement. What will happen?".
The exception will bubble up the stack and your catch
statement would execute if exception type and pattern matching corresponds to the thrown exception.
Upvotes: 1