| Customize Help

Error handling



By default, when an error occurs in a MIL function, a message box is displayed. MIL allows you to control printing errors to the screen using MappControl() with M_ERROR.

An option is available in .NET languages to throw exceptions instead of displaying a message box. Once enabled, when an error occurs in a function, a .NET exception is thrown instead of a message box being displayed. Exceptions thrown by MIL functions are fully compatible with try/catch blocks. To enable exceptions, use MappControl() with M_ERROR set to M_THROW_EXCEPTION. The following example demonstrates how to enable exceptions, and how to use them with a try/catch block.

using System;
using System.Text;

using Matrox.MatroxImagingLibrary;

namespace MILErrors
{
    class MyApplication
    {
        private MIL_ID _milApplication = MIL.M_NULL;
        public MyApplication()
        {
            MIL.MappAlloc(MIL.M_NULL, MIL.M_DEFAULT, ref _milApplication);

            // Tell MIL to throw exceptions when an error occurs.
            // The type of Exception thrown by MIL functions is MILException
            MIL.MappControl(_milApplication, MIL.M_ERROR, MIL.M_THROW_EXCEPTION);
        }
    }

    class MyImage
    {
        private MIL_ID _bufId = MIL.M_NULL;
        public MyImage(MIL_ID ownerSystem, MIL_INT numberOfBands, MIL_INT sizeX, MIL_INT sizeY)
        {
            try
            {
                MIL.MbufAllocColor(ownerSystem,
                                   numberOfBands,
                                   sizeX,
                                   sizeY,
                                   8 + MIL.M_UNSIGNED,
                                   MIL.M_IMAGE + MIL.M_DISP + MIL.M_PROC + MIL.M_GRAB,
                                   ref _bufId);
            }
            catch (MILException exception)
            {
                Console.WriteLine("Image Allocation Error : {0}", exception.Message);
            }
        }
    }
}