Denis Biondic
Denis Biondic

Reputation: 8201

Handle unhandled exceptions only from certain assemblies

Well, maybe a odd question, but how would I handle unhandled exceptions only from certain assemblies in a application, but including exceptions that originate from .NET itself, like for instance when you get a ADO.NET exception it originates from .NET assembly.

I need this because of working with the legacy application where I need unhandled exception handling only for a module consisting of several assemblies, and everything is in the same application in the end, as a part of same process.

Can I maybe catch exceptions on assembly level, instead on the global Application level ?

Upvotes: 3

Views: 1712

Answers (4)

Seb
Seb

Reputation: 2715

No but you can catch at AppDomain level. If the modification is relevant in your application try to :

EDIT :

        AppDomain otherAppDomain = AppDomain.CreateDomain("myDomain");
        otherAppDomain.UnhandledException += new UnhandledExceptionEventHandler(otherAppDomain_UnhandledException);
        Assembly assembly = otherAppDomain.Load("TheAssemblyThatThrows");
        // But you might need to have MyClass inherit from MarshalByRefObject
        MyClass instance = (MyClass)otherAppDomain.CreateInstanceAndUnwrap("TheAssemblyThatThrows", "MyClass");
        instance.DoSomething();

Upvotes: 1

Gert Arnold
Gert Arnold

Reputation: 109253

Of course I don't know the number of calls to the assemblies you want to isolate, so I don't know if this is feasible, but to me it seems that creating a facade around the module you're talking about would be appropriate.

The facade would catch the exceptions that are thrown from these specific assemblies and could rethrow a custom exception with the original exception as InnerException. The custom exception is easily recognized by the global exception handler.

Upvotes: 0

Pedro Costa
Pedro Costa

Reputation: 2996

you could write one from scratch, but http://code.google.com/p/elmah/ is a great framework for handling unhandled exceptions!

you could then use http://code.google.com/p/elmah/wiki/ErrorFiltering to filter per assembly using reflection.

Upvotes: -1

jgauffin
jgauffin

Reputation: 101194

Just check the stack trace and use throw; if you should not handle the exception.

try
{
    //something
}
catch (Exception err)
{
    if (!err.StackTrace.Contains("YourAssemblyName"))
        throw;
}

Upvotes: 3

Related Questions