Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Derive from ApplicationException or not?

I'm confused about when to use ApplicationException. On the page it states

Serves as the base class for application-defined exceptions.

and on the same page there is the following warning

You should derive custom exceptions from the Exception class rather than the ApplicationException class. You should not throw an ApplicationException exception in your code, and you should not catch an ApplicationException exception unless you intend to re-throw the original exception.

So it should serve as a base class for application defined exceptions, but it should not be derived from, nor caught. The warning does not state any further reasoning, unfortunately.

In our application, what we want is to catch all application defined exceptions, and treat them different from exceptions from .net itself. Hence we catch a common exception, from which all application defined exceptions derive.

So instead of doing

catch(AppCustomException ex) { ... }
catch(AnotherAppCustomException ex) { ... }
catch(YetAnotherAppCustomException ex) { ... }

we have

catch(BaseCustomException ex) { ... } //All app custom exceptions derive from this type

I thought it would be good to have ApplicationException as the base for all custom app defined exceptions, instead of BaseCustomException in this example.

A colleague argued we could not use ApplicationException for this common base exception type because of what is stated on MSDN. In particular, that it should not be derived from, nor caught when not retrhown. We don't rethrow these exceptions, for a reason.

So I'm confused now. What is the purpose of ApplicationException really?

like image 429
Mike de Klerk Avatar asked Dec 01 '25 13:12

Mike de Klerk


1 Answers

A whole bunch of exceptions inherit ApplicationException within native .NET libraries. I have no source of proof, but what I understand is that it's intended to be used for application defined exceptions within .NET libraries.

Event it that wasn't true, the problem you'd be facing when creating custom exceptions for your application that derive from ApplicationException is that you might be catching a whole bunch of .NET exceptions you don't care or couldn't even handle. Basically, every catch would need a check to be sure you're catching an exception from your own application rather than .NET.

Take a list of exceptions that derive from ApplicationException within the documentation page you posted a link to.

 Microsoft.JScript.BreakOutOfFinally
 Microsoft.JScript.ContinueOutOfFinally 
 Microsoft.JScript.JScriptException 
 Microsoft.JScript.NoContextException
 Microsoft.JScript.ReturnOutOfFinally
 System.Reflection.InvalidFilterCriteriaException
 System.Reflection.TargetException
 System.Reflection.TargetInvocationException
 System.Reflection.TargetParameterCountException
 System.Threading.WaitHandleCannotBeOpenedException

And the list is not even complete as there are others: DataSourceSerializationException, AppConfigException, NameValidationException and others...

So whenever one of these are thrown, it would hit your catch (ApplicationException exception) while you were expecting an exception from your code.

I would strongly suggest continue using your custom base exception.

like image 121
Imantas Avatar answered Dec 04 '25 06:12

Imantas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!