A practical example of a proactive error handler that automatically defines undeclared variables on the fly, allowing the expression to successfully resume execution.
ID: 1190
See: AddHandler, Error = [ErrorInfo], Error handling and control flow, ErrorCode, ErrorHandlerResponse, Handling Errors, Response = [ErrorHandlerResponse]
using uCalcSoftware;
var uc = new uCalc();
// This handler automatically defines variables when they are first used.
static void AutoDefineHandler(Handle_uCalc h) {
var uc = new uCalc(h);
// Check if the error is specifically an undefined identifier
if (uc.Error.Code == ErrorCode.Undefined_Identifier) {
Console.WriteLine($"Handler: '{uc.Error.Symbol}' is undefined. Defining it now.");
uc.DefineVariable(uc.Error.Symbol);
// Tell the engine to resume the operation
uc.Error.Response = ErrorHandlerResponse.Resume;
}
}
uc.Error.AddHandler(AutoDefineHandler);
// 'my_var' doesn't exist yet, but the handler will create it.
var result = uc.EvalStr("my_var = 100; my_var = my_var * 2");
Console.WriteLine($"Final result: {result}");
Handler: 'my_var' is undefined. Defining it now.
Final result: 200 using uCalcSoftware; var uc = new uCalc(); // This handler automatically defines variables when they are first used. static void AutoDefineHandler(Handle_uCalc h) { var uc = new uCalc(h); // Check if the error is specifically an undefined identifier if (uc.Error.Code == ErrorCode.Undefined_Identifier) { Console.WriteLine($"Handler: '{uc.Error.Symbol}' is undefined. Defining it now."); uc.DefineVariable(uc.Error.Symbol); // Tell the engine to resume the operation uc.Error.Response = ErrorHandlerResponse.Resume; } } uc.Error.AddHandler(AutoDefineHandler); // 'my_var' doesn't exist yet, but the handler will create it. var result = uc.EvalStr("my_var = 100; my_var = my_var * 2"); Console.WriteLine($"Final result: {result}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
// This handler automatically defines variables when they are first used.
void ucalc_call AutoDefineHandler(Handle_uCalc h) {
auto uc = uCalc(h);
// Check if the error is specifically an undefined identifier
if (uc.Error().Code() == ErrorCode::Undefined_Identifier) {
cout << "Handler: '" << uc.Error().Symbol() << "' is undefined. Defining it now." << endl;
uc.DefineVariable(uc.Error().Symbol());
// Tell the engine to resume the operation
uc.Error().Response(ErrorHandlerResponse::Resume);
}
}
int main() {
uCalc uc;
uc.Error().AddHandler(AutoDefineHandler);
// 'my_var' doesn't exist yet, but the handler will create it.
auto result = uc.EvalStr("my_var = 100; my_var = my_var * 2");
cout << "Final result: " << result << endl;
}
Handler: 'my_var' is undefined. Defining it now.
Final result: 200 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; // This handler automatically defines variables when they are first used. void ucalc_call AutoDefineHandler(Handle_uCalc h) { auto uc = uCalc(h); // Check if the error is specifically an undefined identifier if (uc.Error().Code() == ErrorCode::Undefined_Identifier) { cout << "Handler: '" << uc.Error().Symbol() << "' is undefined. Defining it now." << endl; uc.DefineVariable(uc.Error().Symbol()); // Tell the engine to resume the operation uc.Error().Response(ErrorHandlerResponse::Resume); } } int main() { uCalc uc; uc.Error().AddHandler(AutoDefineHandler); // 'my_var' doesn't exist yet, but the handler will create it. auto result = uc.EvalStr("my_var = 100; my_var = my_var * 2"); cout << "Final result: " << result << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
'// This handler automatically defines variables when they are first used.
Public Sub AutoDefineHandler(ByVal h As Handle_uCalc)
Dim uc As New uCalc(h)
'// Check if the error is specifically an undefined identifier
If uc.Error.Code = ErrorCode.Undefined_Identifier Then
Console.WriteLine($"Handler: '{uc.Error.Symbol}' is undefined. Defining it now.")
uc.DefineVariable(uc.Error.Symbol)
'// Tell the engine to resume the operation
uc.Error.Response = ErrorHandlerResponse.Resume
End If
End Sub
Public Sub Main()
Dim uc As New uCalc()
uc.Error.AddHandler(AddressOf AutoDefineHandler)
'// 'my_var' doesn't exist yet, but the handler will create it.
Dim result = uc.EvalStr("my_var = 100; my_var = my_var * 2")
Console.WriteLine($"Final result: {result}")
End Sub
End Module
Handler: 'my_var' is undefined. Defining it now.
Final result: 200 Imports System Imports uCalcSoftware Public Module Program '// This handler automatically defines variables when they are first used. Public Sub AutoDefineHandler(ByVal h As Handle_uCalc) Dim uc As New uCalc(h) '// Check if the error is specifically an undefined identifier If uc.Error.Code = ErrorCode.Undefined_Identifier Then Console.WriteLine($"Handler: '{uc.Error.Symbol}' is undefined. Defining it now.") uc.DefineVariable(uc.Error.Symbol) '// Tell the engine to resume the operation uc.Error.Response = ErrorHandlerResponse.Resume End If End Sub Public Sub Main() Dim uc As New uCalc() uc.Error.AddHandler(AddressOf AutoDefineHandler) '// 'my_var' doesn't exist yet, but the handler will create it. Dim result = uc.EvalStr("my_var = 100; my_var = my_var * 2") Console.WriteLine($"Final result: {result}") End Sub End Module