Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET Try Catch with multiple Catch blocks

This is a really odd issue. We have a Try Catch with multiple Catch blocks. The first Catch block has no code, just a comment.

Try   'Some Code Catch ex As ThreadAbortException   'Do Nothing Catch ex As Exception   HandleException(ex) End Try 

If an exception other than a ThreadAbortException is thrown, it is caught by the second Catch, as expected. However, when stepping through code in VS2010, the ex object is Nothing in that case. So far, we have found two ways to "fix" this issue.

Fix 1: Rename the first exception variable.

Try   'Some Code Catch tex As ThreadAbortException   'Do Nothing Catch ex As Exception   HandleException(ex) End Try 

Fix 2: Add any line of code to the first Catch block.

Try   'Some Code Catch ex As ThreadAbortException   Dim i As Integer = 1 Catch ex As Exception   HandleException(ex) End Try 

The code in HandleException seems to still function properly if it's run, in any of the above cases. Is this a bug in Visual Studio or debugger? Or are we missing something here and the first block of code above is invalid?

This is all being done in .NET 4.0.

like image 743
Dan Avatar asked Jan 17 '13 15:01

Dan


People also ask

Can you have multiple catch blocks within a catch?

Yes you can have multiple catch blocks with try statement. You start with catching specific exceptions and then in the last block you may catch base Exception . Only one of the catch block will handle your exception. You can have try block without a catch block.

Can we throw multiple exception vb net?

You can catch multiple exceptions in a try block, but the exceptions do not get raised simultaneously (at least not in the code you have posted). In other words, the first exception that is raised is the first exception that is caught.

Can we have multiple catches for the same try?

We can have multiple catch blocks for single try block. But only one catch concern catch block gets executed for that try block. No we cannot execute multiple catch blocks for the same try statement. This is because in all cases in case of exception only once the catch statement is executed.

Can I use more than one try block to catch multiple exceptions?

Starting from Java 7.0, it is possible for a single catch block to catch multiple exceptions by separating each with | (pipe symbol) in the catch block. Catching multiple exceptions in a single catch block reduces code duplication and increases efficiency.


2 Answers

Teejay has the correct answer.

However, if your Catch block is empty it makes no sense at all to handle this exception. You just want to prevent the last block from catching it. You can use your method – but consider that having an empty Catch block is normally inacceptable: exceptions should either not be caught, or should be handled properly; swallowing them silently must be seen as a bug. Your case is an exception to this rule but as such it needs to be documented in code since it will confuse careful maintainers otherwise.

Well, VB has a special idiom for exactly this situation:

Try     ' … Catch ex As Exception When Not TypeOf ex Is ThreadAbortException     ' Only executed if `ex` isn’t a ThreadAbortException End Try 

This code doesn’t catch ThreadAbortException at all, which is the right thing to do if you don’t want to handle it: ThreadAbortException cannot be swallowed so even when you catch it, it will be rethrown at the end of the Catch block.

Note that this is fundamentally different from SysDragon’s answer which uses a conventional If statement while the code here uses a special clause in the Catch statement as a filter.

like image 66
Konrad Rudolph Avatar answered Sep 29 '22 07:09

Konrad Rudolph


It seems to be a VS' debugger bug.

PROOF

If you write:

Try     Throw New InvalidOperationException("MESSAGE") Catch ex As ArgumentException     'Do Nothing Catch ex As Exception     Debug.WriteLine(ex) End Try 

and you look at ex it evaluates to Nothing in Quickwatch mode

BUT

in the console the program correctly prints System.InvalidOperationException: MESSAGE

like image 22
Teejay Avatar answered Sep 29 '22 06:09

Teejay