System.ApplicationException

Serves as the base class for application-defined exceptions.

Minimum version: >= 1.1 >= Core 2.0

Statistics

25
elmah.io logo 18

How to handle it

try
{

}
catch (System.ApplicationException e)
{

}
try
{

}
catch (System.ApplicationException e) when (e.Message.Contains("something"))
{

}
try
{

}
catch (System.ApplicationException e) when (LogException(e))
{

}

private static bool LogException(Exception e)
{
    logger.LogError(...);
    return false;
}

How to avoid it

We haven't written anything about avoiding this exception yet. Got a good tip on how to avoid throwing System.ApplicationException? Feel free to reach out through the support widget in the lower right corner with your suggestions.

Links

YouTube videos

Possible fixes from StackOverflow

Inherit from System.Exception. System.ApplicationException is useless and the design guidelines say "Do not throw or derive from System.ApplicationException."

See http://blogs.msdn.com/kcwalina/archive/2006/06/23/644822.aspx

In the C# IDE, type 'exception' and hit TAB. This will expand to get you started in writing a new exception type. There are comments withs links to some discussion of exception practices.

Personally, I'm a big fan of creating lots of small classes, at that extends to exception types. For example, in writing the Foo class, I can choose between:

  1. throw new Exception("Bar happened in Foo");
  2. throw new FooException("Bar happened");
  3. throw new FooBarException();

where

class FooException : Exception 
{
    public FooException(string message) ... 
}

and

class FooBarException : FooException 
{
    public FooBarException() 
        : base ("Bar happened") 
    {
    }
}

I prefer the 3rd option, because I see it as being an OO solution.

You are not handling the case whereby the mutex.WaitOne returns false ie times out. If WaitOne returns false you don't own the mutex therefore you don't need to release it.

bool iOwnTheMutex;

try {
    // set up mutex here...
    iOwnTheMutex = mutex.WaitOne(2000);
    if (iOwnTheMutex) {
       // do what you need to do
    }
}
finally {
    if (mutex != null && iOwnTheMutex) {
       mutex.ReleaseMutex();
    }
}    

You have to be careful when comparing date times because it may seem like they are the same but they can vary down to the ticks (100 nanoseconds). It's probably failing because sql server doesn't store the date times that accurately.

You'll need use a custom equality comparer such that you only compare year, month, day, hour, minute and second probably.

Take a look at this article too: Why datetime cannot compare?

Well stupidely I was using the wrong method on PeristenceSpecification.

I should have been using CheckList not CheckProperty.

Duh!