.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Catching all unhandled exceptions in an AutoCAD Plugin

6 REPLIES 6
Reply
Message 1 of 7
WWFreeman
3038 Views, 6 Replies

Catching all unhandled exceptions in an AutoCAD Plugin

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!");
      }
    }
  }
}
6 REPLIES 6
Message 2 of 7

What about AppDomain.UnhandledException Event?

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 3 of 7

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

Message 4 of 7
norman.yuan
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.

Message 5 of 7
WWFreeman
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!");
      }
    }
  }
}
Message 6 of 7


@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

Message 7 of 7

@ 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?

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost