473,326 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Assembly level exceptoin handler?

Hi all,

I was wondering if it was possible to catch any unhandled exception at
the assembly level. What i'd like to do is catch the exception, wrap
it, log it, and then rethrow it.

It doesn't seem like the GUI should be receiving an ArgumentException
that is thrown from my datalayer, and while I do have try blocks where
i think errors are likely to occur, i'd like to handle the rare
exception in other areas.

It seems like i could use the AppDomain to do this, but then each
layers handler would be triggered when an exception occured in any one.

Thanks
Andy

Nov 17 '05 #1
5 1218
Yes. You can capture it for the purposes of logging it, but can not recover
from it
as well you shouldn't want to because if it was unhandled then you probably
are
not in a stable position to continue. The following code will catch the
unhandled
error and log it to a file, then continue to abort the application.
public class BaseException : Exception, ISerializable
{
public BaseException ()
: base ()
{
}
public BaseException ( string message )
: base ( message )
{

}
public BaseException ( string message, Exception innerException )
: base ( message, innerException )
{
}
protected BaseException ( SerializationInfo info, StreamingContext context )
: base ( info, context )
{
}

}

public sealed class UnhandledExceptionHandler : BaseException
{

public UnhandledExceptionHandler ()
: base ()
{
}

/// <summary>
/// Catches and logs any unhandled exceptions throw in the application.
/// Does NOT handle the error and proceed. The application will still
terminate, but
/// the reasons for the termination will be logged for further review.
/// </summary>
/// <param name="sender"></param>
/// <param name="t"></param>
/// <example>
/// Before Application.Run add the following lines
/// <code>
/// UnhandledExceptionHandler eh = new UnhandledExceptionHandler();
/// AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler ( eh.OnThreadException );
/// </code>
/// </example>
public void OnThreadException ( object sender, UnhandledExceptionEventArgs
t )
{
//Write the information to a log file.
// OpenOrCreateFile is a company method, it just opens a stream after
ensuring it doesn't exist etc.
using ( System.IO.StreamWriter sw = OpenOrCreateFile (
@"C:\UnhandledButCaughtErrors.txt", false ) )
{
Exception ex = null;
sw.Write (
"\r\n============================================= =======\r\n" );
sw.Write ( sender.ToString () + "\r\n" );
sw.Write ( "Caught Error on " + DateTime.Now + "\r\n" );
try
{
ex = (Exception)t.ExceptionObject;
}
catch
{
sw.Write ( "UNABLE TO EXTRACT EXCEPTION FROM '" + t.ToString () +
"'\r\n" );
sw.Write ( "================================================= ===\r\n" );
return;
}
sw.Write ( "Source: " + ex.Source + "\r\n" );
sw.Write ( "Message: " + ex.Message + "\r\n" );
sw.Write ( "TargetSite: " + ex.TargetSite + "\r\n" );
sw.Write ( "Stacktrace: " + ex.StackTrace + "\r\n" );
sw.Write ( "================================================= ===\r\n" );
}
}
}
You would then execute this code before the Application.Run call:

UnhandledExceptionHandler eh = new UnhandledExceptionHandler ();
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler ( eh.OnThreadException );

Nov 17 '05 #2
Thanks gmccallum, thats almost what I'm looking for.

I was looking for something similar that would work in my library
assembly, which doesn't have an entry point or any calls to
Application.Run, so I was hoping there was an assembly attribute or
OnLoad event or something similar to do this with.

Thanks
Andy

Nov 17 '05 #3
Generally speaking, you set up global exception handlers in
applications, not in libraries. It's up to each application to decide
what to do with unhandled exceptions.

That said, if you're writing in-house software, and you just want to
make it easy for other developers (set and forget), then you could do
something like create a static class in your library and set up the
global exception handler in that class's static constructor. Then
create static constructors for the other classes in your library and
have them reference the class that sets up the exception handler.

Since static constructors have to run before any method or property in
the class can run, you're guaranteed that the first reference to any
class in your library will set up the global exception handler.

However, as I said, this would be reasonable only for in-house
software. If your library is to be used in various different contexts,
as the developer of an application using your library I would be pissed
off if I set up my own global exception handler for my specific
application, only to have my global exception handler be replaced by
yours when I make the first call into your library.

Nov 17 '05 #4
Bruce,

Thanks for the feedback. I think I'll abadon this idea, since it seems
more trouble than its worth.

Just for my own information, when you have a statement obj.Event += new
Handler(), I thought that would add another listener to the event, not
replace existing handlers tied to Event. Is that not the case?

Thanks
Andy

Nov 17 '05 #5
Yes, silly me. I wasn't thinking.

Of course your library would just add another listener, not replace the
event handler for the application. My mistake.,

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Edward Diener | last post by:
This has occurred in MC++, but since there is very little response on that NG, I am also reporting it here in the hope that someone can find me a workaround, and report it to MS. If a __value...
0
by: Edward Diener | last post by:
If a __value class with an event is put into an assembly, and a __gc class in another assembly attempts to attach its own event handler to the __value class's event of an embedded object of the...
7
by: Kevin Frey | last post by:
Using .NET 1.1. We have a mixed-mode assembly written in Managed C++ that we are using from an ASP.NET application that has been coded using C#. The mixed-mode assembly has its own...
2
by: Chen Zhuo | last post by:
Hi all experts, We are having a problem with the exact time when a C# dll gets loaded in managed C++. The scenario is like: In managed.cpp: #using MyCSharp.dll
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.