Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying Exception Type in a handler Catch Block

Tags:

c#

exception

I have created custom exception class

public class Web2PDFException : Exception {     public Web2PDFException(string message, Exception innerException)         : base(message, innerException) { ... } } 

In my application how can I find out if it is my custom exception or not?

try {  ...  }  catch (Exception err) { //Find exception type here } 
like image 346
Tomas Avatar asked Nov 26 '09 09:11

Tomas


People also ask

Which type of exception can a catch clause caught?

The compiler checks if the type of the exception thrown is assignable to any of the types declared in the throws clause of the rethrowException method declaration. However, the type of the catch parameter e is Exception , which is a supertype, not a subtype, of FirstException and SecondException .

What kind of exception can be set in the catch block?

C# allows nested try-catch blocks. When using nested try-catch blocks, an exception will be caught in the first matching catch block that follows the try block where an exception occurred. An inner catch block will be executed in the above example because it is the first catch block that handles all exception types.

How do you handle exceptions in catch block?

Place any code statements that might raise or throw an exception in a try block, and place statements used to handle the exception or exceptions in one or more catch blocks below the try block. Each catch block includes the exception type and can contain additional statements needed to handle that exception type.

What is exception Handler exception?

An exception handler is code that stipulates what a program will do when an anomalous event disrupts the normal flow of that program's instructions. An exception, in a computer context, is an unplanned event that occurs while a program is executing and disrupts the flow of its instructions.


2 Answers

When dealing with situations where I don't exactly know what type of exception might come out of a method, a little "trick" I like to do is to recover the Exception's class name and add it to the error log so there is more information.

try {    <code>  } catch ( Exception caughtEx ) {    throw new Exception("Unknown Exception Thrown: "                        + "\n  Type:    " + caughtEx.GetType().Name                        + "\n  Message: " + caughtEx.Message); } 

I do vouch for always handling Exceptions types individually, but the extra bit of info can be helpful, specially when dealing with code from people who love to capture catch-all generic types.

like image 145
Oskuro Avatar answered Oct 01 '22 12:10

Oskuro


UPDATED: assuming C# 6, the chances are that your case can be expressed as an exception filter. This is the ideal approach from a performance perspective assuming your requirement can be expressed in terms of it, e.g.:

try { } catch ( Web2PDFException ex ) when ( ex.Code == 52 ) { } 

Assuming C# < 6, the most efficient is to catch a specific Exception type and do handling based on that. Any catch-all handling can be done separately

try { } catch ( Web2PDFException ex ) { } 

or

try { } catch ( Web2PDFException ex ) { } catch ( Exception ex ) { } 

or (if you need to write a general handler - which is generally a bad idea, but if you're sure it's best for you, you're sure):

 if( err is Web2PDFException)  {  } 

or (in certain cases if you need to do some more complex type hierarchy stuff that cant be expressed with is)

 if( err.GetType().IsAssignableFrom(typeof(Web2PDFException)))  {  } 

or switch to VB.NET or F# and use is or Type.IsAssignableFrom in Exception Filters

like image 21
Ruben Bartelink Avatar answered Oct 01 '22 13:10

Ruben Bartelink