How to define an error handler callback
ID: 1362
See: Rosetta Stone
using uCalcSoftware;
var uc = new uCalc();
// The head, body, callback, and callback:u tags should only be used when the code contains a callback.
// There are two types of callbacks. callback:u is for error handling, while callback is for expression
// functions and operators. They both close with /callback.
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"));
// Note: The head and body tags do NOT come in pairs. Those tags separate sections of code.
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(); // The head, body, callback, and callback:u tags should only be used when the code contains a callback. // There are two types of callbacks. callback:u is for error handling, while callback is for expression // functions and operators. They both close with /callback. 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")); // Note: The head and body tags do NOT come in pairs. Those tags separate sections of code.
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
// The head, body, callback, and callback:u tags should only be used when the code contains a callback.
// There are two types of callbacks. callback:u is for error handling, while callback is for expression
// functions and operators. They both close with /callback.
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;
// Note: The head and body tags do NOT come in pairs. Those tags separate sections of code.
}
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; // The head, body, callback, and callback:u tags should only be used when the code contains a callback. // There are two types of callbacks. callback:u is for error handling, while callback is for expression // functions and operators. They both close with /callback. 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; // Note: The head and body tags do NOT come in pairs. Those tags separate sections of code. }
Imports System
Imports uCalcSoftware
Public Module Program
'// The head, body, callback, and callback:u tags should only be used when the code contains a callback.
'// There are two types of callbacks. callback:u is for error handling, while callback is for expression
'// functions and operators. They both close with /callback.
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"))
'// Note: The head and body tags do NOT come in pairs. Those tags separate sections of code.
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 '// The head, body, callback, and callback:u tags should only be used when the code contains a callback. '// There are two types of callbacks. callback:u is for error handling, while callback is for expression '// functions and operators. They both close with /callback. 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")) '// Note: The head and body tags do NOT come in pairs. Those tags separate sections of code. End Sub End Module