When you select a general exception, how do you determine the original type of exception?

When catching an exception in .NET, you can have as many exception blocks for specific types as possible. But I usually try to have at least one “common” catch block. But is there a way to get the type of “real” exception that was detected by the general exception handler, possibly using reflection?

For example, if I have

Catch ex As System.ServiceModel.FaultException(Of InvalidUser) ProcessModuleLoadException(Me, ex) Catch ex As System.ServiceModel.FaultException(Of SQLExceptions) ProcessModuleLoadException(Me, ex) Catch ex As System.ServiceModel.FaultException(Of DataNullReference) ProcessModuleLoadException(Me, ex) Catch ex As System.ServiceModel.FaultException ProcessModuleLoadException(Me, ex) Catch ex As Exception ProcessModuleLoadException(Me, ex) 

(I broke them off for debugging, although I did the same with every exception).

In short, in “Catch ex As System.ServiceModel.FaultException” I want to examine “ex” and get the base “real” type of exception, either as a type (mainly for debugging, so I can add another catch block) or as a string (for logging).

But inside the Catch "ex" block, a parent class has already been added to it, so any source properties and information about the source exception seem to be lost.

Suggestions?

+4
source share
4 answers

Despite the fact that the Exception been moved to its parent class, you can still call GetType to get the base specific type (pardon my C #):

 try { // Do stuff } catch(Exception ex) { Type exceptionType = ex.GetType(); } 

If this is for diagnostic purposes only, you can write ex.ToString() , which by default includes the base type (in addition to stack tracing, etc.) as follows:

 System.ArgumentException: lastName at Tests.Program.Main(String[] args) in ...\Program.cs:line 22 
+13
source

.GetType will still return its actual type.

+2
source

This is just a small modification to Jeff's answer:

I was looking for something like what Chris describes, and in particular, I wanted to handle the original exception after it was again thrown by a few generic exceptions, and I have only one handler that I could name for all points entrance. And since throwing the exception again will wrap it with its own type of exception, we need to add a loop to go through the internal exceptions and check the first exception thrown.

So here is the code I got into:

 Public Sub HandelException(myEx As Exception) Try Dim InnerEx As Exception = myEx Do Until InnerEx.InnerException Is Nothing InnerEx = InnerEx.InnerException Loop If InnerEx.GetType = GetType(Exception1) Then 'Handle exception type 1 ElseIf InnerEx.GetType = GetType(Exception2) Then 'Handle exception type 2 ElseIf InnerEx.GetType = GetType(Exception3) Then 'Handle exception type 3 ElseIf InnerEx.GetType = GetType(Exception4) Then 'Handle exception type 4 ElseIf InnerEx.GetType = GetType(Exception) Then 'Handle generic system exception End If Catch ex As Exception 'Handel unlikely exception in exception handler LOL. End Try End Sub 

The advantage of this is that you can have a wide selection of types of exceptions that you handle throughout the program, with lots of processing code, but you can handle them all in one place.

I know that this answer was a little late for this question, but I thought I would share it, if possible, to help someone look for the same thing that I was looking for.

+2
source

How about catching it as a general system exception and then using "is" lingo to check the actual type?

 try { // Something } catch(Exception ex) { // Single catch block - catches as generic system exception if(ex is FaultException) { //Process as FaultEx } if(ex is ArithmeticException) { //Process as ArithEx } if(ex is ArgumentException) { //Process as ArgEx } } finally { //Cleanup } 
0
source

All Articles