A practical example using an error handler to provide a custom, user-friendly message when an invalid operation occurs.
ID: 407
using uCalcSoftware;
var uc = new uCalc();
static void MyErrorHandler(Handle_uCalc h) {
var uc = new uCalc(h);
// Check if the specific 'FloatInvalid' error was raised
if (uc.Error.Code == ErrorCode.FloatInvalid) {
Console.WriteLine("Error: The calculation resulted in an invalid number (e.g., square root of a negative).");
// Stop further processing
uc.Error.Response = ErrorHandlerResponse.Abort;
}
}
// Register the custom error handler
uc.Error.AddHandler(MyErrorHandler);
// Tell uCalc to raise an error instead of returning 'nan'
uc.Error.TrapOnInvalid = true;
// This will now trigger our custom error handler's message
uc.EvalStr("sqrt(-4)");
Error: The calculation resulted in an invalid number (e.g., square root of a negative). using uCalcSoftware; var uc = new uCalc(); static void MyErrorHandler(Handle_uCalc h) { var uc = new uCalc(h); // Check if the specific 'FloatInvalid' error was raised if (uc.Error.Code == ErrorCode.FloatInvalid) { Console.WriteLine("Error: The calculation resulted in an invalid number (e.g., square root of a negative)."); // Stop further processing uc.Error.Response = ErrorHandlerResponse.Abort; } } // Register the custom error handler uc.Error.AddHandler(MyErrorHandler); // Tell uCalc to raise an error instead of returning 'nan' uc.Error.TrapOnInvalid = true; // This will now trigger our custom error handler's message uc.EvalStr("sqrt(-4)");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
void ucalc_call MyErrorHandler(Handle_uCalc h) {
auto uc = uCalc(h);
// Check if the specific 'FloatInvalid' error was raised
if (uc.Error().Code() == ErrorCode::FloatInvalid) {
cout << "Error: The calculation resulted in an invalid number (e.g., square root of a negative)." << endl;
// Stop further processing
uc.Error().Response(ErrorHandlerResponse::Abort);
}
}
int main() {
uCalc uc;
// Register the custom error handler
uc.Error().AddHandler(MyErrorHandler);
// Tell uCalc to raise an error instead of returning 'nan'
uc.Error().TrapOnInvalid(true);
// This will now trigger our custom error handler's message
uc.EvalStr("sqrt(-4)");
}
Error: The calculation resulted in an invalid number (e.g., square root of a negative). #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; void ucalc_call MyErrorHandler(Handle_uCalc h) { auto uc = uCalc(h); // Check if the specific 'FloatInvalid' error was raised if (uc.Error().Code() == ErrorCode::FloatInvalid) { cout << "Error: The calculation resulted in an invalid number (e.g., square root of a negative)." << endl; // Stop further processing uc.Error().Response(ErrorHandlerResponse::Abort); } } int main() { uCalc uc; // Register the custom error handler uc.Error().AddHandler(MyErrorHandler); // Tell uCalc to raise an error instead of returning 'nan' uc.Error().TrapOnInvalid(true); // This will now trigger our custom error handler's message uc.EvalStr("sqrt(-4)"); }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub MyErrorHandler(ByVal h As Handle_uCalc)
Dim uc As New uCalc(h)
'// Check if the specific 'FloatInvalid' error was raised
If uc.Error.Code = ErrorCode.FloatInvalid Then
Console.WriteLine("Error: The calculation resulted in an invalid number (e.g., square root of a negative).")
'// Stop further processing
uc.Error.Response = ErrorHandlerResponse.Abort
End If
End Sub
Public Sub Main()
Dim uc As New uCalc()
'// Register the custom error handler
uc.Error.AddHandler(AddressOf MyErrorHandler)
'// Tell uCalc to raise an error instead of returning 'nan'
uc.Error.TrapOnInvalid = true
'// This will now trigger our custom error handler's message
uc.EvalStr("sqrt(-4)")
End Sub
End Module
Error: The calculation resulted in an invalid number (e.g., square root of a negative). Imports System Imports uCalcSoftware Public Module Program Public Sub MyErrorHandler(ByVal h As Handle_uCalc) Dim uc As New uCalc(h) '// Check if the specific 'FloatInvalid' error was raised If uc.Error.Code = ErrorCode.FloatInvalid Then Console.WriteLine("Error: The calculation resulted in an invalid number (e.g., square root of a negative).") '// Stop further processing uc.Error.Response = ErrorHandlerResponse.Abort End If End Sub Public Sub Main() Dim uc As New uCalc() '// Register the custom error handler uc.Error.AddHandler(AddressOf MyErrorHandler) '// Tell uCalc to raise an error instead of returning 'nan' uc.Error.TrapOnInvalid = true '// This will now trigger our custom error handler's message uc.EvalStr("sqrt(-4)") End Sub End Module