Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB Builder NE   

Handling Errors

As with managed code, any errors that occur during execution of an M-function or during data conversion are signaled by a standard .NET exception.

Like any other .NET application, an application that calls a method generated by the MATLAB Builder NE product can handle errors by either

Here are examples for each way of handling errors.

In the GetPrimes example the method itself handles the exception.

public double[] GetPrimes(int n)
{
	MWArray primes= null;
	MyPrimesClass myPrimesClass= null;
	try
	 {
			myPrimesClass= new MyPrimesClass();
			primes= myPrimesClass.myprimes((double)n);
			return (double[])(MWNumericArray)primes).
						  ToVector(MWArrayComponent.Real);
	 }
	catch (Exception ex)
	 {
		Console.WriteLine("Exception: {0}", ex);
		return new double[0];
	 }
}

In the next example, the method that calls myprimes does not catch the exception. Instead, its calling method (that is, the method that calls the method that calls myprimes) handles the exception.

public double[] GetPrimes(int n) 
{ 
   MWArray primes= null; 
   MyPrimesClass myPrimesClass= null; 
   try 
     { 
        myPrimesClass= new MyPrimesClass(); 
        primes= myPrimesClass.myprimes((double)n); 
        return (double[])(MWNumericArray)primes). 
        ToVector(MWArrayComponent.Real); 
     } 

   catch (Exception e) 
     { 
        throw; 
     } 
} 
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS