.NET
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Catching all unhandled exceptions in an AutoCAD Plugin
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Hi again. I am trying to make an universal catcher for all unhandled exceptions (System.Exception) on an AutoCAD plugin. My guess is that a static method is to be hooked on an object, but I haven't figured what that object is for the AutoCAD application. Can someone help me out? Here is a C# test plugin:
namespace TestPlugin {
public class Main : IExtensionApplication {
public void Initialize() {
throw new System.Exception();
}
public void Terminate() {
}
static void AutoPanelExceptionHandler(
object sender,
UnhandledExceptionEventArgs e
) {
if(e != null) {
Autodesk.AutoCAD.ApplicationServices.Application
.DocumentManager.MdiActiveDocument.Editor
.WriteMessage("Exception caught!");
}
}
}
}
Re: Catching all unhandled exceptions in an AutoCAD Plugin
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
What about AppDomain.UnhandledException Event?
Re: Catching all unhandled exceptions in an AutoCAD Plugin
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
'unhandled' means that there is no active exception handler at the point when an exception is raised.
Within the handlers of registered commands and lisp functions, AutoCAD's managed API which invokes the CommandMethods and LispFunctions is handling any exception that's not otherwise handled by the CommandMethod or LispFunction being invoked.
Hence, the UnhandledException event will never be raised, in those cases. In some other cases, where AutoCAD does not have an exception handler active (most events, overrule methods, and so forth), you may see that event fire, but otherwise you must handle the exceptions explicitly (and of course, you could route them throuh some static object that fires an event like UnhandledException)
Re: Catching all unhandled exceptions in an AutoCAD Plugin
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
<quote>
public class Main : IExtensionApplication {
public void Initialize() {
throw new System.Exception();
}
</quote>
This is bad code to test: AutoCAD silently swallow any unhandled exception raised in IExtensionApplication.Initialize() and make entire DLL assembly useless.
Re: Catching all unhandled exceptions in an AutoCAD Plugin
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
@ norman.yuan
Yeah, now I am unable to edit the original post. I guess this one is better. AutoCAD takes the exception and displays its own modal dialogue. My question seems to be if that exception can be intercepted globally before AutoCAD fetches it.
using System;
using Autodesk.AutoCAD.Runtime;
[assembly: CommandClass(typeof(TestPlugin.Main))]
namespace TestPlugin {
public class Main : IExtensionApplication {
[CommandMethod("throwexception", CommandFlags.UsePickSet)]
public static void testException() {
throw new System.Exception();
}
public void Initialize() {
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(AutoPanelExceptionH andler);
}
public void Terminate() {
}
static void AutoPanelExceptionHandler(
object sender,
UnhandledExceptionEventArgs e
) {
if(e != null) {
Autodesk.AutoCAD.ApplicationServices.Application
.DocumentManager.MdiActiveDocument.Editor
.WriteMessage("Exception caught!");
}
}
}
}
Re: Catching all unhandled exceptions in an AutoCAD Plugin
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
WWFreeman wrote:@ norman.yuan
Yeah, now I am unable to edit the original post. I guess this one is better. AutoCAD takes the exception and displays its own modal dialogue. My question seems to be if that exception can be intercepted globally before AutoCAD fetches it.caught!");
If you're trying to handle exceptions that would otherwise not be handled by your code, I don't think there's any way to hook into AutoCAD's exception handler.
On Framework V4.0 later, you can examine (but not handle) most exceptions using the AppDomain's FirstChanceException event.
See http://msdn.microsoft.com/en-us/library/dd997368.aspx
Re: Catching all unhandled exceptions in an AutoCAD Plugin
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
@ DiningPhilosopher
"If you're trying to handle exceptions that would otherwise not be handled by your code, I don't think there's any way to hook into AutoCAD's exception handler."
This is correct. I am actually trying to avoid adding a generic exception handler to each and every command. By the looks of this topic, that seems to be an unavailable option.
Have I gotten it correctly?



