.NET

Reply
Active Contributor
WWFreeman
Posts: 27
Registered: ‎09-02-2012
Message 1 of 7 (541 Views)

Catching all unhandled exceptions in an AutoCAD Plugin

541 Views, 6 Replies
01-25-2013 02:38 AM

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!");
      }
    }
  }
}
Moderator
Alexander.Rivilis
Posts: 1,388
Registered: ‎04-09-2008
Message 2 of 7 (527 Views)

Re: Catching all unhandled exceptions in an AutoCAD Plugin

01-25-2013 03:17 AM in reply to: WWFreeman

What about AppDomain.UnhandledException Event?


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 3 of 7 (514 Views)

Re: Catching all unhandled exceptions in an AutoCAD Plugin

01-25-2013 05:30 AM in reply to: WWFreeman

'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)

*Expert Elite*
norman.yuan
Posts: 946
Registered: ‎04-27-2009
Message 4 of 7 (502 Views)

Re: Catching all unhandled exceptions in an AutoCAD Plugin

01-25-2013 06:46 AM in reply to: WWFreeman

<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.

Active Contributor
WWFreeman
Posts: 27
Registered: ‎09-02-2012
Message 5 of 7 (493 Views)

Re: Catching all unhandled exceptions in an AutoCAD Plugin

01-25-2013 07:22 AM in reply to: norman.yuan

@ 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(AutoPanelExceptionHandler);
    }

    public void Terminate() {
    }

    static void AutoPanelExceptionHandler(
      object sender,
      UnhandledExceptionEventArgs e
    ) {
      if(e != null) {
        Autodesk.AutoCAD.ApplicationServices.Application
        .DocumentManager.MdiActiveDocument.Editor
        .WriteMessage("Exception caught!");
      }
    }
  }
}
Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 6 of 7 (485 Views)

Re: Catching all unhandled exceptions in an AutoCAD Plugin

01-25-2013 08:21 AM in reply to: WWFreeman

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

Active Contributor
WWFreeman
Posts: 27
Registered: ‎09-02-2012
Message 7 of 7 (480 Views)

Re: Catching all unhandled exceptions in an AutoCAD Plugin

01-25-2013 08:31 AM in reply to: DiningPhilosopher

@ 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. :smileyhappy: Have I gotten it correctly?

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community