可以参考这个方法:
Public Function MapExceptionToNumber(e As Exception) As Integer
dim type As Type = e.GetType()
If (type Is GetType(System.IndexOutOfRangeException))
Return 9
End If
If (type Is GetType(System.RankException))
Return 9
End If
If (type Is GetType(System.DivideByZeroException))
Return 11
End If
If (type Is GetType(System.OverflowException))
Return 6
End If
If (type Is GetType(System.NotFiniteNumberException))
If (CType(e, NotFiniteNumberException).OffendingNumber = 0.0d)
Return 11
Else
Return 6
End If
End If
If (type Is GetType(System.NullReferenceException))
Return 91
End If
If (type Is GetType(System.InvalidCastException))
Return 13
End If
If (type Is GetType(System.NotSupportedException))
Return 13
End If
If (type Is GetType(System.Runtime.InteropServices.SEHException))
Return 99
End If
If (type Is GetType(System.DllNotFoundException))
Return 53
End If
If (type Is GetType(System.EntryPointNotFoundException))
Return 453
End If
If (type Is GetType(System.TypeLoadException))
Return 429
End If
If (type Is GetType(System.OutOfMemoryException))
Return 7
End If
If (type Is GetType(System.FormatException) )
Return 13
End If
If (type Is GetType(System.IO.DirectoryNotFoundException))
Return 76
End If
If (type Is GetType(System.IO.IOException))
Return 57
End If
If (type Is GetType(System.IO.FileNotFoundException))
Return 53
End If
If (TypeOf e is System.MissingMemberException)
Return 438
End If
If(TypeOf e is System.Runtime.InteropServices.InvalidOleVariantTypeException)
Return 5
End If
Return 458
End Function