Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you catch a Exception<T> Where T is any type?

I want to be able to catch a WebFaultException<string> as the most specific then a WebFaultException<T> (T being any other type) as a more general case to handle. Is this possible?

try {     // throw exception } catch (WebFaultException<string> e) {     // handle more specific type } catch (WebFaultException<T> e) {     // handle more general type } catch (Exception e) { } 
like image 830
Despertar Avatar asked Jan 27 '12 06:01

Despertar


People also ask

Does catch exception catch everything?

If you catch some Exception types and don't do anything with the information, you have no chance of knowing what went wrong in those situations, but if you catch all Exception subclasses you have no chance of knowing what went wrong in a much large number of situations.

How do you catch any kind of exception?

Exception handling is used to handle the exceptions. We can use try catch block to protect the code. Catch block is used to catch all types of exception. The keyword “catch” is used to catch exceptions.

Which exception Cannot be caught in catch block?

The only one that cannot be caught is StackOverflowException , and TreadAbortException gets rethrown at the end of the catch.

Does except exception catch all exceptions?

Try and Except Statement – Catching all ExceptionsTry and except statements are used to catch and handle exceptions in Python. Statements that can raise exceptions are kept inside the try clause and the statements that handle the exception are written inside except clause.


2 Answers

As other answers have mentioned; you cannot directly specify to catch every WebFaultException<T> without either knowing the specified type argument, or catching it's base type instead.

However, if you were wanting to catch all WebFaultException occurrences and handle the response differently based on what the generic type is, then you could simply catch the base type and use reflection to determine the type of the generic argument using Type.GetGenericArguments(). Here is a simple catch clause to show how you may go about doing it:

// ...  catch (FaultException ex) {     Type exceptionType = ex.GetType();      if (exceptionType.IsGenericType)     {         // Find the type of the generic parameter         Type genericType = ex.GetType().GetGenericArguments().FirstOrDefault();          if (genericType != null)         {             // TODO: Handle the generic type.         }     } } 

If you want a more indepth example, I have uploaded a test program to demonstrate this to PasteBin. Feel free to take a look!

like image 89
Samuel Slade Avatar answered Sep 26 '22 19:09

Samuel Slade


I've never thought of anything like this, so I got curious and did a little scratch pad implementation. First off, the following works:

public class WebFaultException : Exception {     public WebFaultException() { }     public WebFaultException(string message) : base(message) { }     public WebFaultException(string message, Exception innerException) : base(message, innerException) { }     protected WebFaultException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { } }  public class WebFaultException<T> : WebFaultException {     public WebFaultException() { }     public WebFaultException(string message) : base(message) { }     public WebFaultException(string message, Exception innerException) : base(message, innerException) {  }     protected WebFaultException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) {} } 

As your exception definitions and then these tests both pass:

    [TestMethod]     public void SpecificGeneric()     {         bool hitException = false;         try         {             throw new WebFaultException<string>();         }         catch(WebFaultException<string> e)         {             hitException = true;         }          Assert.IsTrue(hitException);     }      [TestMethod]     public void AnyGeneric()     {         bool hitException = false;         try         {             throw new WebFaultException<int>();         }         catch (WebFaultException<string> e)         {             hitException = false;         }         catch (WebFaultException e)         {             hitException = true;         }          Assert.IsTrue(hitException);     } 

In terms of doing what you want specifically, there's a catch. In the code as you presented it, WebDefaultException<T> is meaningless because you have provided no T. However, you can get around that (somewhat awkwardly) like this (another passing unit test):

    [TestMethod]     public void CallingGenericMethod()     {         Assert.IsTrue(GenericExceptionMethod<int>());     }      private bool GenericExceptionMethod<T>()     {         bool hitException = false;         try         {             throw new WebFaultException<int>();         }         catch (WebFaultException<string> e)         {             hitException = false;         }         catch (WebFaultException<T> e)         {             hitException = true;         }         return hitException;     } 

That is, if the method (or class) in which you're handling the exception has a generic parameter, you can actually catch WebFaultException<T>. However, I would urge a word of caution that this is a very weird thing to do -- as a client of your method, I'm forced to pass in a type that will be used for nothing that I care about and is an internal implementation detail to you for some exception that you want to catch.

So, I'd say yes, possible. But also awkward at best and perhaps ill-advised.

like image 45
Erik Dietrich Avatar answered Sep 22 '22 19:09

Erik Dietrich