uCalc API Version: 2.1.3-preview.2 Released: 6/16/2026
Warning
uCalc API Preview Release Notice:The documentation describes the intended behavior of the API. The current preview build contains incomplete features, unoptimized performance, and is subject to breaking changes.
Message = [string]
Property
Product:
Class:
Retrieves the descriptive message for the last error that occurred.
Remarks
The Message property is a key part of uCalc's state-based error handling model. It provides a simple way to inspect error details without relying on traditional exception handling, which can be advantageous in performance-critical loops.
It returns the message for the most recent error triggered by an operation like EvalStr(), Parse(), or Define(). If the last operation was successful, it returns the message for ErrorCode::None ("No error").
⚙️ Error State Lifecycle
It is crucial to understand that uCalc's error state (including the error message, number, and location) is transient. The state is automatically cleared and reset by the next operation that is initiated.
For example, if EvalStr("1/0") fails, ErrorMessage() will return "Division by 0". If the next call is EvalStr("2+2"), which succeeds, the error state is cleared, and ErrorMessage() will subsequently return "No error".
🆚 Comparative Analysis: State-Based Errors vs. Exceptions
Most modern languages use a try/catch mechanism for error handling. This pattern interrupts the normal flow of execution and unwinds the stack to find a handler.
Traditional try/catch (e.g., C#, C++):
// C# Exampletry{ result = PerformCalculation(input);}catch (Exception ex){ Console.WriteLine(ex.Message); // Message is tied to the exception object}uCalc's State-Based Model:
// uCalc's approachuc.EvalStr("some invalid input");if (uc.Error.GetCode() != ErrorCode.None) {{ Console.WriteLine(uc.Error.GetMessage()); // Message is retrieved from the uCalc instance's state}uCalc's approach avoids the overhead associated with throwing and catching exceptions, making it suitable for scenarios where invalid user input is common and performance is a priority. The program flow is not interrupted, allowing the developer to check for an error state and react accordingly.
Examples
Demonstrates getting the last error message after a failed operation.
using uCalcSoftware;
var uc = new uCalc();
// Attempt to evaluate an expression with unbalanced parenthesis causing a syntax error.
uc.EvalStr("5 * (10 +");
// Check the error message from the last operation.
Console.WriteLine($"Last error message: {uc.Error.Message}");
Last error message: Bracket delimiter error using uCalcSoftware; var uc = new uCalc(); // Attempt to evaluate an expression with unbalanced parenthesis causing a syntax error. uc.EvalStr("5 * (10 +"); // Check the error message from the last operation. Console.WriteLine($"Last error message: {uc.Error.Message}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// Attempt to evaluate an expression with unbalanced parenthesis causing a syntax error.
uc.EvalStr("5 * (10 +");
// Check the error message from the last operation.
cout << "Last error message: " << uc.Error().Message() << endl;
}
Last error message: Bracket delimiter error #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // Attempt to evaluate an expression with unbalanced parenthesis causing a syntax error. uc.EvalStr("5 * (10 +"); // Check the error message from the last operation. cout << "Last error message: " << uc.Error().Message() << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// Attempt to evaluate an expression with unbalanced parenthesis causing a syntax error.
uc.EvalStr("5 * (10 +")
'// Check the error message from the last operation.
Console.WriteLine($"Last error message: {uc.Error.Message}")
End Sub
End Module
Last error message: Bracket delimiter error Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// Attempt to evaluate an expression with unbalanced parenthesis causing a syntax error. uc.EvalStr("5 * (10 +") '// Check the error message from the last operation. Console.WriteLine($"Last error message: {uc.Error.Message}") End Sub End Module
Retrieves the generic message for a specific error code and compares it to the last error message.
using uCalcSoftware;
var uc = new uCalc();
// Retrieve the built-in message for a specific error code, without triggering an error.
Console.WriteLine($"Generic 'Undefined Identifier' message: {uc.Error.GetMessage(ErrorCode.Undefined_Identifier)}");
// Now, trigger a specific error by using an undefined variable.
uc.EvalStr("MyUndefinedVar + 5");
// Check the message and number for the last error that occurred.
Console.WriteLine($"Last error message: {uc.Error.Message}");
Console.WriteLine($"Last error number: {(int)uc.Error.Code}");
Generic 'Undefined Identifier' message: Undefined identifier
Last error message: Undefined identifier
Last error number: 258 using uCalcSoftware; var uc = new uCalc(); // Retrieve the built-in message for a specific error code, without triggering an error. Console.WriteLine($"Generic 'Undefined Identifier' message: {uc.Error.GetMessage(ErrorCode.Undefined_Identifier)}"); // Now, trigger a specific error by using an undefined variable. uc.EvalStr("MyUndefinedVar + 5"); // Check the message and number for the last error that occurred. Console.WriteLine($"Last error message: {uc.Error.Message}"); Console.WriteLine($"Last error number: {(int)uc.Error.Code}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// Retrieve the built-in message for a specific error code, without triggering an error.
cout << "Generic 'Undefined Identifier' message: " << uc.Error().GetMessage(ErrorCode::Undefined_Identifier) << endl;
// Now, trigger a specific error by using an undefined variable.
uc.EvalStr("MyUndefinedVar + 5");
// Check the message and number for the last error that occurred.
cout << "Last error message: " << uc.Error().Message() << endl;
cout << "Last error number: " << (int)uc.Error().Code() << endl;
}
Generic 'Undefined Identifier' message: Undefined identifier
Last error message: Undefined identifier
Last error number: 258 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // Retrieve the built-in message for a specific error code, without triggering an error. cout << "Generic 'Undefined Identifier' message: " << uc.Error().GetMessage(ErrorCode::Undefined_Identifier) << endl; // Now, trigger a specific error by using an undefined variable. uc.EvalStr("MyUndefinedVar + 5"); // Check the message and number for the last error that occurred. cout << "Last error message: " << uc.Error().Message() << endl; cout << "Last error number: " << (int)uc.Error().Code() << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// Retrieve the built-in message for a specific error code, without triggering an error.
Console.WriteLine($"Generic 'Undefined Identifier' message: {uc.Error.GetMessage(ErrorCode.Undefined_Identifier)}")
'// Now, trigger a specific error by using an undefined variable.
uc.EvalStr("MyUndefinedVar + 5")
'// Check the message and number for the last error that occurred.
Console.WriteLine($"Last error message: {uc.Error.Message}")
Console.WriteLine($"Last error number: {CType(uc.Error.Code, Integer)}")
End Sub
End Module
Generic 'Undefined Identifier' message: Undefined identifier
Last error message: Undefined identifier
Last error number: 258 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// Retrieve the built-in message for a specific error code, without triggering an error. Console.WriteLine($"Generic 'Undefined Identifier' message: {uc.Error.GetMessage(ErrorCode.Undefined_Identifier)}") '// Now, trigger a specific error by using an undefined variable. uc.EvalStr("MyUndefinedVar + 5") '// Check the message and number for the last error that occurred. Console.WriteLine($"Last error message: {uc.Error.Message}") Console.WriteLine($"Last error number: {CType(uc.Error.Code, Integer)}") End Sub End Module
Internal Test: Verifies that the error state is correctly cleared after a subsequent successful operation.
using uCalcSoftware;
var uc = new uCalc();
// Internal Test: Verify error state is cleared
Console.WriteLine("--- Testing Error State Lifecycle ---");
// 1. Trigger a division-by-zero error.
uc.Error.TrapOnDivideByZero = true;
uc.EvalStr("1 / 0");
Console.WriteLine($"Message after 1/0: '{uc.Error.Message}'");
Console.WriteLine($"Error number is not None: {uc.Error.Code != ErrorCode.None}");
// 2. Perform a successful operation, which should clear the previous error state.
uc.EvalStr("1 + 1");
// 3. Verify the error message and number have been reset.
Console.WriteLine($"Message after successful op: '{uc.Error.Message}'");
Console.WriteLine($"Error number is now None: {uc.Error.Code == ErrorCode.None}");
--- Testing Error State Lifecycle ---
Message after 1/0: 'Division by 0'
Error number is not None: True
Message after successful op: 'No error'
Error number is now None: True using uCalcSoftware; var uc = new uCalc(); // Internal Test: Verify error state is cleared Console.WriteLine("--- Testing Error State Lifecycle ---"); // 1. Trigger a division-by-zero error. uc.Error.TrapOnDivideByZero = true; uc.EvalStr("1 / 0"); Console.WriteLine($"Message after 1/0: '{uc.Error.Message}'"); Console.WriteLine($"Error number is not None: {uc.Error.Code != ErrorCode.None}"); // 2. Perform a successful operation, which should clear the previous error state. uc.EvalStr("1 + 1"); // 3. Verify the error message and number have been reset. Console.WriteLine($"Message after successful op: '{uc.Error.Message}'"); Console.WriteLine($"Error number is now None: {uc.Error.Code == ErrorCode.None}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
#define tf(IsTrue) ((IsTrue) ? "True" : "False")
int main() {
uCalc uc;
// Internal Test: Verify error state is cleared
cout << "--- Testing Error State Lifecycle ---" << endl;
// 1. Trigger a division-by-zero error.
uc.Error().TrapOnDivideByZero(true);
uc.EvalStr("1 / 0");
cout << "Message after 1/0: '" << uc.Error().Message() << "'" << endl;
cout << "Error number is not None: " << tf(uc.Error().Code() != ErrorCode::None) << endl;
// 2. Perform a successful operation, which should clear the previous error state.
uc.EvalStr("1 + 1");
// 3. Verify the error message and number have been reset.
cout << "Message after successful op: '" << uc.Error().Message() << "'" << endl;
cout << "Error number is now None: " << tf(uc.Error().Code() == ErrorCode::None) << endl;
}
--- Testing Error State Lifecycle ---
Message after 1/0: 'Division by 0'
Error number is not None: True
Message after successful op: 'No error'
Error number is now None: True #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; #define tf(IsTrue) ((IsTrue) ? "True" : "False") int main() { uCalc uc; // Internal Test: Verify error state is cleared cout << "--- Testing Error State Lifecycle ---" << endl; // 1. Trigger a division-by-zero error. uc.Error().TrapOnDivideByZero(true); uc.EvalStr("1 / 0"); cout << "Message after 1/0: '" << uc.Error().Message() << "'" << endl; cout << "Error number is not None: " << tf(uc.Error().Code() != ErrorCode::None) << endl; // 2. Perform a successful operation, which should clear the previous error state. uc.EvalStr("1 + 1"); // 3. Verify the error message and number have been reset. cout << "Message after successful op: '" << uc.Error().Message() << "'" << endl; cout << "Error number is now None: " << tf(uc.Error().Code() == ErrorCode::None) << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// Internal Test: Verify error state is cleared
Console.WriteLine("--- Testing Error State Lifecycle ---")
'// 1. Trigger a division-by-zero error.
uc.Error.TrapOnDivideByZero = true
uc.EvalStr("1 / 0")
Console.WriteLine($"Message after 1/0: '{uc.Error.Message}'")
Console.WriteLine($"Error number is not None: {uc.Error.Code <> ErrorCode.None}")
'// 2. Perform a successful operation, which should clear the previous error state.
uc.EvalStr("1 + 1")
'// 3. Verify the error message and number have been reset.
Console.WriteLine($"Message after successful op: '{uc.Error.Message}'")
Console.WriteLine($"Error number is now None: {uc.Error.Code = ErrorCode.None}")
End Sub
End Module
--- Testing Error State Lifecycle ---
Message after 1/0: 'Division by 0'
Error number is not None: True
Message after successful op: 'No error'
Error number is now None: True Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// Internal Test: Verify error state is cleared Console.WriteLine("--- Testing Error State Lifecycle ---") '// 1. Trigger a division-by-zero error. uc.Error.TrapOnDivideByZero = true uc.EvalStr("1 / 0") Console.WriteLine($"Message after 1/0: '{uc.Error.Message}'") Console.WriteLine($"Error number is not None: {uc.Error.Code <> ErrorCode.None}") '// 2. Perform a successful operation, which should clear the previous error state. uc.EvalStr("1 + 1") '// 3. Verify the error message and number have been reset. Console.WriteLine($"Message after successful op: '{uc.Error.Message}'") Console.WriteLine($"Error number is now None: {uc.Error.Code = ErrorCode.None}") End Sub End Module
Adding an error handler callback
using uCalcSoftware;
var uc = new uCalc();
static void MyErrorHandler(Handle_uCalc h) {
var uc = new uCalc(h);
Console.WriteLine("An error has occurred!");
Console.WriteLine($"Error #: {(int)uc.Error.Code}");
Console.WriteLine($"Error Message: {uc.Error.Message}");
Console.WriteLine($"Error Symbol: {uc.Error.Symbol}");
Console.WriteLine($"Error Location: {uc.Error.Location}");
Console.WriteLine($"Error Expression: {uc.Error.Expression}");
}
uc.Error.AddHandler(MyErrorHandler);
Console.WriteLine(uc.EvalStr("123+"));
Console.WriteLine("");
uc.Error.TrapOnDivideByZero = true;
Console.WriteLine(uc.EvalStr("5/0"));
An error has occurred!
Error #: 257
Error Message: Syntax error
Error Symbol: +
Error Location: 3
Error Expression: 123+
Syntax error
An error has occurred!
Error #: 8
Error Message: Division by 0
Error Symbol:
Error Location: 0
Error Expression:
Division by 0 using uCalcSoftware; var uc = new uCalc(); static void MyErrorHandler(Handle_uCalc h) { var uc = new uCalc(h); Console.WriteLine("An error has occurred!"); Console.WriteLine($"Error #: {(int)uc.Error.Code}"); Console.WriteLine($"Error Message: {uc.Error.Message}"); Console.WriteLine($"Error Symbol: {uc.Error.Symbol}"); Console.WriteLine($"Error Location: {uc.Error.Location}"); Console.WriteLine($"Error Expression: {uc.Error.Expression}"); } uc.Error.AddHandler(MyErrorHandler); Console.WriteLine(uc.EvalStr("123+")); Console.WriteLine(""); uc.Error.TrapOnDivideByZero = true; Console.WriteLine(uc.EvalStr("5/0"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
void ucalc_call MyErrorHandler(Handle_uCalc h) {
auto uc = uCalc(h);
cout << "An error has occurred!" << endl;
cout << "Error #: " << (int)uc.Error().Code() << endl;
cout << "Error Message: " << uc.Error().Message() << endl;
cout << "Error Symbol: " << uc.Error().Symbol() << endl;
cout << "Error Location: " << uc.Error().Location() << endl;
cout << "Error Expression: " << uc.Error().Expression() << endl;
}
int main() {
uCalc uc;
uc.Error().AddHandler(MyErrorHandler);
cout << uc.EvalStr("123+") << endl;
cout << "" << endl;
uc.Error().TrapOnDivideByZero(true);
cout << uc.EvalStr("5/0") << endl;
}
An error has occurred!
Error #: 257
Error Message: Syntax error
Error Symbol: +
Error Location: 3
Error Expression: 123+
Syntax error
An error has occurred!
Error #: 8
Error Message: Division by 0
Error Symbol:
Error Location: 0
Error Expression:
Division by 0 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; void ucalc_call MyErrorHandler(Handle_uCalc h) { auto uc = uCalc(h); cout << "An error has occurred!" << endl; cout << "Error #: " << (int)uc.Error().Code() << endl; cout << "Error Message: " << uc.Error().Message() << endl; cout << "Error Symbol: " << uc.Error().Symbol() << endl; cout << "Error Location: " << uc.Error().Location() << endl; cout << "Error Expression: " << uc.Error().Expression() << endl; } int main() { uCalc uc; uc.Error().AddHandler(MyErrorHandler); cout << uc.EvalStr("123+") << endl; cout << "" << endl; uc.Error().TrapOnDivideByZero(true); cout << uc.EvalStr("5/0") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub MyErrorHandler(ByVal h As Handle_uCalc)
Dim uc As New uCalc(h)
Console.WriteLine("An error has occurred!")
Console.WriteLine($"Error #: {CInt(uc.Error.Code)}")
Console.WriteLine($"Error Message: {uc.Error.Message}")
Console.WriteLine($"Error Symbol: {uc.Error.Symbol}")
Console.WriteLine($"Error Location: {uc.Error.Location}")
Console.WriteLine($"Error Expression: {uc.Error.Expression}")
End Sub
Public Sub Main()
Dim uc As New uCalc()
uc.Error.AddHandler(AddressOf MyErrorHandler)
Console.WriteLine(uc.EvalStr("123+"))
Console.WriteLine("")
uc.Error.TrapOnDivideByZero = true
Console.WriteLine(uc.EvalStr("5/0"))
End Sub
End Module
An error has occurred!
Error #: 257
Error Message: Syntax error
Error Symbol: +
Error Location: 3
Error Expression: 123+
Syntax error
An error has occurred!
Error #: 8
Error Message: Division by 0
Error Symbol:
Error Location: 0
Error Expression:
Division by 0 Imports System Imports uCalcSoftware Public Module Program Public Sub MyErrorHandler(ByVal h As Handle_uCalc) Dim uc As New uCalc(h) Console.WriteLine("An error has occurred!") Console.WriteLine($"Error #: {CInt(uc.Error.Code)}") Console.WriteLine($"Error Message: {uc.Error.Message}") Console.WriteLine($"Error Symbol: {uc.Error.Symbol}") Console.WriteLine($"Error Location: {uc.Error.Location}") Console.WriteLine($"Error Expression: {uc.Error.Expression}") End Sub Public Sub Main() Dim uc As New uCalc() uc.Error.AddHandler(AddressOf MyErrorHandler) Console.WriteLine(uc.EvalStr("123+")) Console.WriteLine("") uc.Error.TrapOnDivideByZero = true Console.WriteLine(uc.EvalStr("5/0")) End Sub End Module