Numeric Exception in D365 - X++ Code

Rumman Ansari   Software Engineer   2023-05-26   498 Share
☰ Table of Contents

Table of Content:


Numeric Exception in D365 - X++ Code

try and catch block


class ExceptionHandling
{


    public static void main(Args ar)
    {
      int x, y, z;
      x = 10;
      y = 0;

      try
      {
        z=x/y;
        Info(strFmt("%1",z));
      } 
      catch (Exception::Numeric)
      {
        Info("numeric exception");
       }

    }
}

Output:

Numeric Exception in D365 - X++ Code

Multiple catch - With throw block


class ExceptionHandling
{
 
    public static void main(Args ar)
    {
       int x,y;

       x = 10;
       y = 30;

       try
       {
         if (x < y)
              throw Exception: :Warning:
       }
       catch(Exception::warning)
       {
         warning("x is less");
       }
       catch (Exception::error)
       {
         error("x is less");
       }
       catch(Exception::info)
       {
         info("x is less");
       } 

    }
}

Output:

Numeric Exception in D365 - X++ Code

retry


class ExceptionHandling
{
 
    public static void main(Args ar)
    {
       int x,y;

       x = 10;
       y = 30;

       try
       {
         if (x < y)
              throw Exception: :error:
         Info(strFmt("%1",x));
       }
       catch(Exception::warning)
       {
         warning("x is less");
       }
       catch (Exception::error)
       {
         error("x is less");
         
         x = 40;
         retry;
       }
       catch(Exception::info)
       {
         info("x is less");
       } 

    }
}

Output: It will print 40

Numeric Exception in D365 - X++ Code

finally Block


class ExceptionHandling
{
 
    public static void main(Args ar)
    {
       int x,y;

       x = 10;
       y = 30;

       try
       {
         if (x < y)
              throw Exception: :error:
         Info(strFmt("%1",x));
       }
       catch(Exception::warning)
       {
         warning("x is less");
       }
       catch (Exception::error)
       {
         error("x is less"); 
       }
       catch(Exception::info)
       {
         info("x is less");
       }
       finally
        {
           Info("final block");
       }

    }
}

Output:

finally