.NET

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

Catching all unhandled exceptions in an AutoCAD Plugin

674 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,431
Registered: ‎04-09-2008
Message 2 of 7 (660 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 (647 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: 1,016
Registered: ‎04-27-2009
Message 4 of 7 (635 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 (626 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 (618 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 (613 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?

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.