Internal Test: Demonstrates error recovery by having an error handler resume execution after a custom error is raised.
ID: 484
using uCalcSoftware;
var uc = new uCalc();
static void RecoveryHandler(Handle_uCalc h) {
var uc = new uCalc(h);
Console.WriteLine($"Handler: Caught '{uc.Error.Message}'");
// Attempt to recover by resuming execution.
uc.Error.Response = ErrorHandlerResponse.Resume;
Console.WriteLine("Handler: Resuming execution...");
}
static void RiskyOperation(uCalc.Callback cb) {
var input = cb.ArgStr(1);
if (input == "bad") {
cb.Error.Raise("A recoverable error occurred.");
// After the error handler resumes, this return value will be used.
} else {
cb.ReturnStr("Normal_OK");
}
}
uc.Error.AddHandler(RecoveryHandler);
uc.DefineFunction("DoWork(s As String) As String", RiskyOperation);
Console.WriteLine("Result: " + uc.EvalStr("DoWork('good')"));
Console.WriteLine("---");
Console.WriteLine("Result: " + uc.EvalStr("DoWork('bad')"));
Result: Normal_OK
---
Handler: Caught 'A recoverable error occurred.'
Handler: Resuming execution...
Result: A recoverable error occurred. using uCalcSoftware; var uc = new uCalc(); static void RecoveryHandler(Handle_uCalc h) { var uc = new uCalc(h); Console.WriteLine($"Handler: Caught '{uc.Error.Message}'"); // Attempt to recover by resuming execution. uc.Error.Response = ErrorHandlerResponse.Resume; Console.WriteLine("Handler: Resuming execution..."); } static void RiskyOperation(uCalc.Callback cb) { var input = cb.ArgStr(1); if (input == "bad") { cb.Error.Raise("A recoverable error occurred."); // After the error handler resumes, this return value will be used. } else { cb.ReturnStr("Normal_OK"); } } uc.Error.AddHandler(RecoveryHandler); uc.DefineFunction("DoWork(s As String) As String", RiskyOperation); Console.WriteLine("Result: " + uc.EvalStr("DoWork('good')")); Console.WriteLine("---"); Console.WriteLine("Result: " + uc.EvalStr("DoWork('bad')"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
void ucalc_call RecoveryHandler(Handle_uCalc h) {
auto uc = uCalc(h);
cout << "Handler: Caught '" << uc.Error().Message() << "'" << endl;
// Attempt to recover by resuming execution.
uc.Error().Response(ErrorHandlerResponse::Resume);
cout << "Handler: Resuming execution..." << endl;
}
void ucalc_call RiskyOperation(uCalcBase::Callback cb) {
auto input = cb.ArgStr(1);
if (input == "bad") {
cb.Error().Raise("A recoverable error occurred.");
// After the error handler resumes, this return value will be used.
} else {
cb.ReturnStr("Normal_OK");
}
}
int main() {
uCalc uc;
uc.Error().AddHandler(RecoveryHandler);
uc.DefineFunction("DoWork(s As String) As String", RiskyOperation);
cout << "Result: " + uc.EvalStr("DoWork('good')") << endl;
cout << "---" << endl;
cout << "Result: " + uc.EvalStr("DoWork('bad')") << endl;
}
Result: Normal_OK
---
Handler: Caught 'A recoverable error occurred.'
Handler: Resuming execution...
Result: A recoverable error occurred. #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; void ucalc_call RecoveryHandler(Handle_uCalc h) { auto uc = uCalc(h); cout << "Handler: Caught '" << uc.Error().Message() << "'" << endl; // Attempt to recover by resuming execution. uc.Error().Response(ErrorHandlerResponse::Resume); cout << "Handler: Resuming execution..." << endl; } void ucalc_call RiskyOperation(uCalcBase::Callback cb) { auto input = cb.ArgStr(1); if (input == "bad") { cb.Error().Raise("A recoverable error occurred."); // After the error handler resumes, this return value will be used. } else { cb.ReturnStr("Normal_OK"); } } int main() { uCalc uc; uc.Error().AddHandler(RecoveryHandler); uc.DefineFunction("DoWork(s As String) As String", RiskyOperation); cout << "Result: " + uc.EvalStr("DoWork('good')") << endl; cout << "---" << endl; cout << "Result: " + uc.EvalStr("DoWork('bad')") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub RecoveryHandler(ByVal h As Handle_uCalc)
Dim uc As New uCalc(h)
Console.WriteLine($"Handler: Caught '{uc.Error.Message}'")
'// Attempt to recover by resuming execution.
uc.Error.Response = ErrorHandlerResponse.Resume
Console.WriteLine("Handler: Resuming execution...")
End Sub
Public Sub RiskyOperation(ByVal cb As uCalc.Callback)
Dim input = cb.ArgStr(1)
If input = "bad" Then
cb.Error.Raise("A recoverable error occurred.")
'// After the error handler resumes, this return value will be used.
Else
cb.ReturnStr("Normal_OK")
End If
End Sub
Public Sub Main()
Dim uc As New uCalc()
uc.Error.AddHandler(AddressOf RecoveryHandler)
uc.DefineFunction("DoWork(s As String) As String", AddressOf RiskyOperation)
Console.WriteLine("Result: " + uc.EvalStr("DoWork('good')"))
Console.WriteLine("---")
Console.WriteLine("Result: " + uc.EvalStr("DoWork('bad')"))
End Sub
End Module
Result: Normal_OK
---
Handler: Caught 'A recoverable error occurred.'
Handler: Resuming execution...
Result: A recoverable error occurred. Imports System Imports uCalcSoftware Public Module Program Public Sub RecoveryHandler(ByVal h As Handle_uCalc) Dim uc As New uCalc(h) Console.WriteLine($"Handler: Caught '{uc.Error.Message}'") '// Attempt to recover by resuming execution. uc.Error.Response = ErrorHandlerResponse.Resume Console.WriteLine("Handler: Resuming execution...") End Sub Public Sub RiskyOperation(ByVal cb As uCalc.Callback) Dim input = cb.ArgStr(1) If input = "bad" Then cb.Error.Raise("A recoverable error occurred.") '// After the error handler resumes, this return value will be used. Else cb.ReturnStr("Normal_OK") End If End Sub Public Sub Main() Dim uc As New uCalc() uc.Error.AddHandler(AddressOf RecoveryHandler) uc.DefineFunction("DoWork(s As String) As String", AddressOf RiskyOperation) Console.WriteLine("Result: " + uc.EvalStr("DoWork('good')")) Console.WriteLine("---") Console.WriteLine("Result: " + uc.EvalStr("DoWork('bad')")) End Sub End Module