Best vb.net questions in March 2012

Which exceptions should a program never attempt to recover from?

28 votes

Exceptions can have different degrees of impact on a program. For example a program should probably abort if OutOfMemoryException is raised, but it is possible to safely and appropriately handle System.Data.SqlClient.SqlException without putting the program in an unknown state.

I do understand that any exception has the potential to put the program in an unstable state if it is not properly handled. Are there exceptions that should never be handled beyond simply logging and throwing up the stack?

The Framework Design Guidelines cover this pretty completely:

Do not catch System.Exception or System.SystemException in framework code, unless you intend to re-throw.

...

Do not catch System.StackOverflowException.

It is extremely difficult to programmatically handle a stack overflow. You should allow this exception to terminate the process and use debugging to determine the source of the problem.

...

Do not catch System.Runtime.InteropServices.SEHException explicitly.

Does VB.NET have functionality similar to TSQL's "IN"?

5 votes

In TSQL..

IF MyVal IN (1, 2, 3, 4, 14) BEGIN ... END

Is there a way to do this in VB.NET?

Is it possible to check for the existence of an integer in a set of integers inline?

Such as:

If MyVal in (1, 2, 3, 4, 14) Then ... End If

Arrays are an implementation of IEnumerable so with the System.Linq import a shorthand version of Tim Schmelter's answer would be:

{1,2,3,4,14}.Contains(MyVal)

Arrays also have an explicit implementation of IList.Contains, so without LINQ a perhaps less elegant alternative is:

DirectCast({1,2,3,4,14}, IList).Contains(MyVal)