General Rules of Try..Catch

SRSDS
Advisor
Advisor

General Rules of Try..Catch

SRSDS
Advisor
Advisor

I'm using Try.. Catch error handlers at the start and end of subroutines but often errors aren't catched.

 

 

A simple example is if array(i) throws an exception

'array(i)' threw an exception of type 'System.IndexOutOfRangeException'

 

 

Do I need to put Try.. Catch in intermediate sections of SubRoutines?

Are there any rules to follow?

 

0 Likes
Reply
394 Views
2 Replies
Replies (2)

ActivistInvestor
Advisor
Advisor
Can you show an example of where try and catch does't trap an IndexOutOfRange exception?

0 Likes

norman.yuan
Mentor
Mentor

In AutoCAD .NET API, we do run into cases from time to time where AutoCAD crashes even the code that causes the exception is wrapped in try...catch... block, but they usually happens with specific AutoCAD operation with abnormal situation, such as corrupted drawing file, or handling AutoCAD object in wrong way. It depends on how the .NET API wrapped the underline ObjectARX stuff.

 

But saying try...catch... does not catching array's IndexOutOfRange exception? I can only assume something very unusual with your code.

 

Here is simplified code to raise and handle array's IndexOutRange exception that works correctly, as expected:

using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(MiscTest.MyCommands))]

namespace MiscTest
{
    public class MyCommands
    {
        [CommandMethod("CatchError1")]
        public static void CatchErrorFromOutside()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;

            try
            {
                RaiseArrayOutOfRangeException();
            }
            catch (System.Exception ex)
            {
                CadApp.ShowAlertDialog($"Error caught from outside routine:\n{ex.Message}");
            }
            finally
            {
                Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
            }
        }

        [CommandMethod("CatchError2")]
        public static void CatchErrorFromInside()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;

            try
            {
                CatchArrayOutOfRangeExceptionFromInsideRoutine();
            }
            catch (System.Exception ex)
            {
                //This "catch..." clause should not be executed
                CadApp.ShowAlertDialog($"Unhandled error from inside:\n{ex.Message}");
            }
            finally
            {
                Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
            }
        }

        private static void RaiseArrayOutOfRangeException()
        {
            var numbers = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            for (int i=0; i<=20; i++)
            {
                //When i>9, an "Out of range" exception will be raised
                string val = numbers[i].ToString();
            }
        }

        private static void CatchArrayOutOfRangeExceptionFromInsideRoutine()
        {
            try
            {
                RaiseArrayOutOfRangeException();
            }
            catch (System.Exception ex)
            {
                CadApp.ShowAlertDialog($"Error caught from inside routine:\n{ex.Message}");
            }
        }
    }
}

You may want to post your code to back up your claim.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes