ErrorMessageMethod

Applies to:uCalc Transform
Class:uCalc
Returns a message associated with the error that was raised
Syntax
ErrorMessage(Index)
Parameters
Index
(Default = Err_None)
Index of error message to return
Returns
Type: String
Returns a message associated with the error that was raised
Remarks
If an error was raised, this returns a message describing the recent error. If the Index argument is specified, then the error message associated with this Index is returned. A value must be chosen from ErrorNumberEnum.

Note: Errors are cleared whenever a new expression is parsed or a new item is defined.

Example 1: Defining a constant

uc.DefineVariable("x = 10")
uc.DefineConstant("Pi = 3.14")

uc.EvalStr("x = 20")
Console.WriteLine(uc.ErrorMessage()) ' Returns "No error"

uc.EvalStr("Pi = 25")
Console.WriteLine(uc.ErrorMessage()) ' Returns "Value cannot be assigned here"

Console.WriteLine(uc.EvalStr("x")) ' Returns 20
Console.WriteLine(uc.EvalStr("Pi")) ' Returns 3.14

          

uc.DefineVariable("x = 10");
uc.DefineConstant("Pi = 3.14");

uc.EvalStr("x = 20");
Console.WriteLine(uc.ErrorMessage()); // Returns "No error";

uc.EvalStr("Pi = 25");
Console.WriteLine(uc.ErrorMessage()); // Returns "Value cannot be assigned here";

Console.WriteLine(uc.EvalStr("x")); // Returns 20;
Console.WriteLine(uc.EvalStr("Pi")); // Returns 3.14;

uc.GetItemOf("Pi").Release();

          

      uc.DefineVariable('x = 10');
      uc.DefineConstant('Pi = 3.14');

      uc.EvalStr('x = 20');
      WriteLn(uc.ErrorMessage()); // Returns 'No error';

      uc.EvalStr('Pi = 25');
      WriteLn(uc.ErrorMessage()); // Returns 'Value cannot be assigned here';

      WriteLn(uc.EvalStr('x')); // Returns 20;
      WriteLn(uc.EvalStr('Pi')); // Returns 3.14;

          

uc.DefineVariable("x = 10");
uc.DefineConstant("Pi = 3.14");

uc.EvalStr("x = 20");
cout << uc.ErrorMessage() << endl; // Returns "No error";

uc.EvalStr("Pi = 25");
cout << uc.ErrorMessage() << endl; // Returns "Value cannot be assigned here";

cout << uc.EvalStr("x") << endl; // Returns 20;
cout << uc.EvalStr("Pi") << endl; // Returns 3.14;

uc.GetItemOf("Pi").Release();

          

uc.DefineVariable("x = 10");
uc.DefineConstant("Pi = 3.14");

uc.EvalStr("x = 20");
Console::WriteLine(uc.ErrorMessage()); // Returns "No error";

uc.EvalStr("Pi = 25");
Console::WriteLine(uc.ErrorMessage()); // Returns "Value cannot be assigned here";

Console::WriteLine(uc.EvalStr("x")); // Returns 20;
Console::WriteLine(uc.EvalStr("Pi")); // Returns 3.14;

uc.GetItemOf("Pi").Release();

          
Example 2: Adding error handlers

Sub ErrorHandlerTestCB(ByVal ucPtr As IntPtr)
   Dim ucc As New uCalc(ucPtr)
   Console.WriteLine("An error has occured!")
   Console.Write("Error #: "): Console.WriteLine(ucc.ErrorNumber())
   Console.WriteLine("Error Message: " + ucc.ErrorMessage())
   Console.WriteLine("Error Symbol: " + ucc.ErrorSymbol())
   Console.WriteLine("Error Location: " + ucc.ErrorLocation())
   Console.WriteLine("Error Expression: " + ucc.ErrorExpression())
End Sub

Sub AutoVariableDefCB(ByVal ucPtr As IntPtr)
   Dim ucc As New uCalc(ucPtr)
   if ucc.ErrorNumber() = ErrorNumberEnum.Err_Undefined_Identifier Then 
      ucc.DefineVariable(ucc.ErrorSymbol())
      ucc.ErrorResponse(ErrorHandlerResponseEnum.ErrorResume)
   End if
End Sub

Sub ErrorHandler_()' +++ example also for 2nd arg
   uc.AddErrorHandler(uc.PinAddr(AddressOf ErrorHandlerTestCB))
   uc.AddErrorHandler(uc.PinAddr(AddressOf AutoVariableDefCB))
   Console.WriteLine(uc.EvalStr("1+")) ' Returns "Syntax error"
   ' Output
   ' An error has occured!
   ' Error #: 2
   ' Error Message: Syntax error
   ' Error Symbol: +
   ' Error Location: 1
   ' Error Expression: 1+

   Console.WriteLine(uc.EvalStr("5/0")) ' Returns "Division by 0"
   ' Output
   ' An error has occured!
   ' Error #: 20
   ' Error Message: Division by 0
   ' Error Symbol: 
   ' Error Location: 0
   ' Error Expression: 

   uc.Eval("AutoTest = 123")
   Console.WriteLine(uc.Eval("AutoTest * 1000")) ' Returns 123000
End Sub

          

static void ErrorHandlerTestCB(IntPtr ucPtr) {
   uCalc ucc = New uCalc(ucPtr);
   Console.WriteLine("An error has occured!");
   Console.Write("Error #: "); Console.WriteLine(ucc.ErrorNumber());
   Console.WriteLine("Error Message: " + ucc.ErrorMessage());
   Console.WriteLine("Error Symbol: " + ucc.ErrorSymbol());
   Console.WriteLine("Error Location: " + ucc.ErrorLocation());
   Console.WriteLine("Error Expression: " + ucc.ErrorExpression());
}

static void AutoVariableDefCB(IntPtr ucPtr) {
   uCalc ucc = New uCalc(ucPtr);
   if (ucc.ErrorNumber() == ErrorNumberEnum.Err_Undefined_Identifier) {
      ucc.DefineVariable(ucc.ErrorSymbol());
      ucc.ErrorResponse(ErrorHandlerResponseEnum.ErrorResume);
   }
}

static void ErrorHandler_() { // +++ example also for 2nd arg
   uc.AddErrorHandler(uc.PinAddr(ErrorHandlerTestCB));
   uc.AddErrorHandler(uc.PinAddr(AutoVariableDefCB));
   Console.WriteLine(uc.EvalStr("1+")); // Returns "Syntax error";
   // Output
   // An error has occured!
   // Error #: 2
   // Error Message: Syntax error
   // Error Symbol: +
   // Error Location: 1
   // Error Expression: 1+
   
   Console.WriteLine(uc.EvalStr("5/0")); // Returns "Division by 0";
   // Output
   // An error has occured!
   // Error #: 20
   // Error Message: Division by 0
   // Error Symbol: 
   // Error Location: 0
   // Error Expression: 
   
   uc.Eval("AutoTest = 123");
   Console.WriteLine(uc.Eval("AutoTest * 1000")); // Returns 123000;
}

          

procedure ErrorHandlerTestCB(ucPtr: System.Pointer);
begin
   
//ucc uCalc.Create(ucPtr);;
   WriteLn('An error has occured!');
   Write('Error #: ');; WriteLn(ucc.ErrorNumber());
   WriteLn('Error Message: ' + ucc.ErrorMessage());
   WriteLn('Error Symbol: ' + ucc.ErrorSymbol());
   WriteLn('Error Location: ' + ucc.ErrorLocation());
   WriteLn('Error Expression: ' + ucc.ErrorExpression());
End;

procedure AutoVariableDefCB(ucPtr: System.Pointer);
begin
   
//ucc uCalc.Create(ucPtr);;
   if ucc.ErrorNumber() = ErrorNumberEnum.Err_Undefined_Identifier Then 
begin
   
      ucc.DefineVariable(ucc.ErrorSymbol());
      ucc.ErrorResponse(ErrorHandlerResponseEnum.ErrorResume);

End;
End;

procedure ErrorHandler_();
begin
   // +++ example also for 2nd arg
   uc.AddErrorHandler(ErrorHandlerTestCB);
   uc.AddErrorHandler(AutoVariableDefCB);
   WriteLn(uc.EvalStr('1+')); // Returns 'Syntax error';
   // Output
// An error has occured!
// Error #: 2
// Error Message: Syntax error
// Error Symbol: +
// Error Location: 1
// Error Expression: 1+

   WriteLn(uc.EvalStr('5/0')); // Returns 'Division by 0';
   // Output
// An error has occured!
// Error #: 20
// Error Message: Division by 0
// Error Symbol: 
// Error Location: 0
// Error Expression: 

   uc.Eval('AutoTest = 123');
   WriteLn(uc.Eval('AutoTest * 1000')); // Returns 123000;
End;

          

void _stdcall ErrorHandlerTestCB(uCalcPtr ucPtr) {
   uCalc ucc(ucPtr);
   cout << "An error has occured!" << endl;
   cout << "Error #: "; cout << ucc.ErrorNumber() << endl;
   cout << "Error Message: " << ucc.ErrorMessage() << endl;
   cout << "Error Symbol: " << ucc.ErrorSymbol() << endl;
   cout << "Error Location: " << ucc.ErrorLocation() << endl;
   cout << "Error Expression: " << ucc.ErrorExpression() << endl;
}

void _stdcall AutoVariableDefCB(uCalcPtr ucPtr) {
   uCalc ucc(ucPtr);
   if (ucc.ErrorNumber() == ErrorNumberEnum::Err_Undefined_Identifier) {
      ucc.DefineVariable(ucc.ErrorSymbol());
      ucc.ErrorResponse(ErrorHandlerResponseEnum::ErrorResume);
   }
}

void ErrorHandler_() { // +++ example also for 2nd arg
   uc.AddErrorHandler(ErrorHandlerTestCB);
   uc.AddErrorHandler(AutoVariableDefCB);
   cout << uc.EvalStr("1+") << endl; // Returns "Syntax error";
   // Output
   // An error has occured!
   // Error #: 2
   // Error Message: Syntax error
   // Error Symbol: +
   // Error Location: 1
   // Error Expression: 1+
   
   cout << uc.EvalStr("5/0") << endl; // Returns "Division by 0";
   // Output
   // An error has occured!
   // Error #: 20
   // Error Message: Division by 0
   // Error Symbol: 
   // Error Location: 0
   // Error Expression: 
   
   uc.Eval("AutoTest = 123");
   cout << uc.Eval("AutoTest * 1000") << endl; // Returns 123000;
}

          

static void ErrorHandlerTestCB(uCalcPtr ucPtr) {
   uCalc ucc(ucPtr);
   Console::WriteLine("An error has occured!");
   Console::Write("Error #: "); Console::WriteLine(ucc.ErrorNumber());
   Console::WriteLine("Error Message: " + ucc.ErrorMessage());
   Console::WriteLine("Error Symbol: " + ucc.ErrorSymbol());
   Console::WriteLine("Error Location: " + ucc.ErrorLocation());
   Console::WriteLine("Error Expression: " + ucc.ErrorExpression());
}

static void AutoVariableDefCB(uCalcPtr ucPtr) {
   uCalc ucc(ucPtr);
   if (ucc.ErrorNumber() == ErrorNumberEnum::Err_Undefined_Identifier) {
      ucc.DefineVariable(ucc.ErrorSymbol());
      ucc.ErrorResponse(ErrorHandlerResponseEnum::ErrorResume);
   }
}

static void ErrorHandler_() { // +++ example also for 2nd arg
   uc.AddErrorHandler(ucPinAddr(ErrorHandlerTestCB));
   uc.AddErrorHandler(ucPinAddr(AutoVariableDefCB));
   Console::WriteLine(uc.EvalStr("1+")); // Returns "Syntax error";
   // Output
   // An error has occured!
   // Error #: 2
   // Error Message: Syntax error
   // Error Symbol: +
   // Error Location: 1
   // Error Expression: 1+
   
   Console::WriteLine(uc.EvalStr("5/0")); // Returns "Division by 0";
   // Output
   // An error has occured!
   // Error #: 20
   // Error Message: Division by 0
   // Error Symbol: 
   // Error Location: 0
   // Error Expression: 
   
   uc.Eval("AutoTest = 123");
   Console::WriteLine(uc.Eval("AutoTest * 1000")); // Returns 123000;
}

          
DLL import code
<DllImport(uCalcDLL, CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Cdecl, EntryPoint:="ErrorMessage")> _

Private Function ErrorMessage__(ByVal uCalcHandle As IntPtr, ByVal Index As ErrorNumberEnum) As IntPtr
End Function
            
[DllImport(uCalcDLL, CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl, EntryPoint="ErrorMessage")]

protected static extern  IntPtr ErrorMessage_(IntPtr uCalcHandle, ErrorNumberEnum Index  );
            
{DLLImport}function ErrorMessage__(uCalcHandle: System.Pointer; Index: ErrorNumberEnum):  PAnsiChar; cdecl; external uCalcDLL name 'ErrorMessage';

            
typedef const char * (* __ErrorMessage)(void *uCalcHandle,  ErrorNumberEnum Index  ); 

            
[DllImport(uCalcLib, CharSet=CharSet::Ansi, CallingConvention=CallingConvention::Cdecl, EntryPoint = "ErrorMessage")]

static STR_RETURN ErrorMessage_(void *  uCalcHandle,  ErrorNumberEnum Index);
            
See also
Prev | Next