• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Active Contributor
    Posts: 27
    Registered: ‎09-02-2012

    Catching all unhandled exceptions in an AutoCAD Plugin

    257 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!");
          }
        }
      }
    }
    Please use plain text.
    Moderator
    Alexander.Rivilis
    Posts: 1,168
    Registered: ‎04-09-2008

    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

    Please use plain text.
    Valued Mentor
    Posts: 322
    Registered: ‎05-06-2012

    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)

    Please use plain text.
    *Expert Elite*
    Posts: 683
    Registered: ‎04-27-2009

    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.

    Please use plain text.
    Active Contributor
    Posts: 27
    Registered: ‎09-02-2012

    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!");
          }
        }
      }
    }
    Please use plain text.
    Valued Mentor
    Posts: 322
    Registered: ‎05-06-2012

    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

    Please use plain text.
    Active Contributor
    Posts: 27
    Registered: ‎09-02-2012

    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?

    Please use plain text.