Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to exceptions thrown inside a catch filter (when clause) in C#? Can they be traced?

Tags:

c#

I was reading up on filter expressions using catch(..) when (..) in C# and I couldn't find any mention of what happens if an exception is thrown while evaluating the expression. I tried going through the docs and spec but either I couldn't find them or they are not mentioned. So, I made up a tiny program to check what happens when I throw from when(..). I put it up on https://dotnetfiddle.net/R7oJNp

try {
    Console.WriteLine("Test(): Inside Test()");
    throw new Exception("thrown from Test()");
} catch (Exception) when (AlwaysThrow()) {
    Console.WriteLine("Test(): caught exception in Test()");
} catch (Exception e) {
    Console.WriteLine("Test(): totally skipped last catch clause, any inner exception: " + (e.InnerException?.Message ?? "nothing"));
    throw;
}

What I'm noticing is that the entire catch(..) when (..) block is skipped if an exception is thrown inside when(..). It skips to the next matching catch block and upon inspecting the exception object caught, there's no trace of the exception that originated from when(..).

I wanted to know if this is the expected behavior and if there's a reference to the docs/spec regarding what's supposed to happen in this scenario. It seems odd to skip the entire block and throw away the exception because that'd make it very hard to debug.

**Edit: ** Okay, this is the behavior according to .NET docs but is there any way to trace these? I'm onboard with the idea that exceptions must not happen and filters should be simple enough but we make mistakes. Also, isn't this behavior supposed to be mentioned in C# spec?

like image 217
sangeeth96 Avatar asked Sep 26 '18 10:09

sangeeth96


People also ask

What happens if exception occurs in catch block?

Answer: When an exception is thrown in the catch block, then the program will stop the execution. In case the program has to continue, then there has to be a separate try-catch block to handle the exception raised in the catch block.

Can you throw an exception in a catch block?

A throw statement can be used in a catch block to re-throw the exception that is caught by the catch statement. The following example extracts source information from an IOException exception, and then throws the exception to the parent method. You can catch one exception and throw a different exception.

What will happen if exception occurs in finally block in C#?

And the second line that is written after the exception occur is not printed because at the point where the Exception occured the execution will stop and the statements after the exception will not be executed. So the "finally" block execution stops at the point where the exception is thrown.

When should you Rethrow an exception?

If a catch block cannot handle the particular exception it has caught, you can rethrow the exception. The rethrow expression ( throw without assignment_expression) causes the originally thrown object to be rethrown.


1 Answers

VB.Net has supported this long before C# so you may be searching too narrowly:

If an exception occurs during execution of the user-filtered expression, that exception is discarded and the filter expression is considered to have evaluated to false.

https://learn.microsoft.com/en-us/dotnet/standard/exceptions/using-user-filtered-exception-handlers

like image 181
Crowcoder Avatar answered Oct 17 '22 02:10

Crowcoder