<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Throwing exception on plugin load in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/throwing-exception-on-plugin-load/m-p/12956112#M2976</link>
    <description>&lt;P&gt;I'm trying to check some config criteria in my plugin's Initialize method and throw an error if it's not met. I'm currently running a method that does the checks and returns a bool, then in my init I'm throwing a custom exception if the check returns false.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My plugin correctly fails to load if the checks are not passed, but the exception is not displayed on the console like other plugin exceptions are.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do I need to use some specific AutoCAD-centric way of throwing an exception to the AutoCAD console?&lt;/P&gt;</description>
    <pubDate>Tue, 13 Aug 2024 14:30:35 GMT</pubDate>
    <dc:creator>ChrisClems</dc:creator>
    <dc:date>2024-08-13T14:30:35Z</dc:date>
    <item>
      <title>Throwing exception on plugin load</title>
      <link>https://forums.autodesk.com/t5/net-forum/throwing-exception-on-plugin-load/m-p/12956112#M2976</link>
      <description>&lt;P&gt;I'm trying to check some config criteria in my plugin's Initialize method and throw an error if it's not met. I'm currently running a method that does the checks and returns a bool, then in my init I'm throwing a custom exception if the check returns false.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My plugin correctly fails to load if the checks are not passed, but the exception is not displayed on the console like other plugin exceptions are.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do I need to use some specific AutoCAD-centric way of throwing an exception to the AutoCAD console?&lt;/P&gt;</description>
      <pubDate>Tue, 13 Aug 2024 14:30:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/throwing-exception-on-plugin-load/m-p/12956112#M2976</guid>
      <dc:creator>ChrisClems</dc:creator>
      <dc:date>2024-08-13T14:30:35Z</dc:date>
    </item>
    <item>
      <title>Re: Throwing exception on plugin load</title>
      <link>https://forums.autodesk.com/t5/net-forum/throwing-exception-on-plugin-load/m-p/12956154#M2977</link>
      <description>&lt;P&gt;I assume you do your check and throw custom exception in IExtensionApplication.Initialize(). The code in this Initialize() should ALWAYS handle possible exception (i.e. using try...catch... do allow the code execution goes to the end of Initialize(). If an Exception is not handled, AutoCAD does not crash, but silently fail the DLL loading (which is what you have seen to your DLL). Unhandled exception in Initialize() that fails DLL loading is a well-known topic and has been discussed many times in this forum.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, if you want to do your check in the Initialize(), you do not throw an exception and leave it for AutoCAD to swallow. Say, you can save the result in some way and wait AutoCAD to complete the startup (for example, handling Application.idle event) and then prompt user for the undesired check result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Aug 2024 14:45:05 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/throwing-exception-on-plugin-load/m-p/12956154#M2977</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2024-08-13T14:45:05Z</dc:date>
    </item>
    <item>
      <title>Re: Throwing exception on plugin load</title>
      <link>https://forums.autodesk.com/t5/net-forum/throwing-exception-on-plugin-load/m-p/12956189#M2978</link>
      <description>&lt;P&gt;I agree with &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/543921"&gt;@norman.yuan&lt;/a&gt;&amp;nbsp; Here's some code I got from others. In the initialize event, do your test. You can throw an error, but catch it and use that to send a message to the command line. In this sample, any unhandled error sends and "Initialization error: " message. You can of course make it more specific. Just add a catch clause for your specific error.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;using Autodesk.AutoCAD.ApplicationServices;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;

// This line is not mandatory, but improves loading performances
[assembly: ExtensionApplication(typeof(EJ_.MyPlugin))]

namespace EJ_
{
    /// This class is instantiated by AutoCAD once and kept alive for the 
    /// duration of the session. If you don't do any one time initialization 
    /// then you should remove this class.

    public class MyPlugin : IExtensionApplication
    {

        void IExtensionApplication.Initialize()
        {
            AcAp.Idle += OnIdle;
        }

        void IExtensionApplication.Terminate()
        {
            // Do plug-in application clean up here
        }
        private void OnIdle(object sender, EventArgs e)
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            if (doc != null)
            {
                Application.Idle -= OnIdle;
                try
                {
                    doc.Editor.WriteMessage("\nEJ_PageSetupImport loaded.\n");
                }
                catch (System.Exception ex)
                {
                    doc.Editor.WriteMessage($"\nInitilization error: {ex.Message}");
                }                
            }
        }
    }
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp; &lt;/P&gt;</description>
      <pubDate>Tue, 13 Aug 2024 15:00:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/throwing-exception-on-plugin-load/m-p/12956189#M2978</guid>
      <dc:creator>Ed__Jobe</dc:creator>
      <dc:date>2024-08-13T15:00:45Z</dc:date>
    </item>
    <item>
      <title>Re: Throwing exception on plugin load</title>
      <link>https://forums.autodesk.com/t5/net-forum/throwing-exception-on-plugin-load/m-p/12956655#M2979</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/11123300"&gt;@ChrisClems&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;I'm trying to check some config criteria in my plugin's Initialize method and throw an error if it's not met. I'm currently running a method that does the checks and returns a bool, then in my init I'm throwing a custom exception if the check returns false.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;My plugin correctly fails to load if the checks are not passed&lt;/STRONG&gt;&lt;/EM&gt;, but the exception is not displayed on the console like other plugin exceptions are.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do I need to use some specific AutoCAD-centric way of throwing an exception to the AutoCAD console?&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;At the point when your Initialize() method is called, your plug-in &lt;EM&gt;is already loaded&lt;/EM&gt;, so it is not failing to load, it is only preventing AutoCAD from registering its commands, which is fine if that is your intent, but you cannot prevent the plug-in from loading in the first place. So, if you don't want your commands registered, then you &lt;EM&gt;must&lt;/EM&gt; throw an exception in Initialize().&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, because AutoCAD traps and suppresses exceptions thrown from Initialize(), there is no indication that something has gone wrong, other than that your commands were not registered. This behavior is something that many AutoCAD .NET developers have an issue with, and has caused numerous headaches due to the AutoCAD managed API's use of smoke-signals to indicate that a failure occurred in an IExtensionApplication's Initialize() method.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What you can do,&amp;nbsp;&lt;EM&gt;before&lt;/EM&gt; you throw the exception, is something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;YourCustomException exceptionToThrow = new YourCustomException(...)
UnhandledExceptionFilter.CerOrShowExceptionDialog(exceptionToThrow);
throw exceptionToThrow;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only issue with the above is that it will trip up automation and scripting because it requires user intervention. If you don't want to require the user to click on a dialog button to continue, you can instead do this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;YourCustomException exceptionToThrow = new YourCustomException(...)
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(exceptionToThrow.Message);
throw exceptionToThrow;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If your plug-in only defines commands, then the above is most-likely all you need to do. OTOH, if your plug-in has a UI (e.g., PaletteSet or Ribbon content); adds handlers to events; or creates and enables Overrules when it loads, the problem becomes a bit more complicated, but can still be solved.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the case that some of those other things are done when your plug-in loads, you can add a public static member variable to your IExtensionApplication-based class, of type bool, and assign the result of your validation method to it, to indicate if there was a failure or not. You can then check that variable before taking any other actions like adding content to the ribbon; adding handlers to events; creating Overrules; and so on.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Aug 2024 02:07:28 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/throwing-exception-on-plugin-load/m-p/12956655#M2979</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-08-14T02:07:28Z</dc:date>
    </item>
  </channel>
</rss>

