How to reactively check for an error after an operation fails, and how a subsequent successful operation clears the error state.
ID: 1189
See: Handling Errors
using uCalcSoftware;
var uc = new uCalc();
// Trigger a syntax error
uc.EvalStr("1 +");
// Check the error state after the fact
if (uc.Error.Code != ErrorCode.None) {
Console.WriteLine($"Error detected: {uc.Error.Message}");
} else {
Console.WriteLine("Success!");
}
// Perform a successful operation, which clears the error state
uc.EvalStr("1 + 1");
if (uc.Error.Code == ErrorCode.None) {
Console.WriteLine("Previous error was cleared by successful operation.");
}
Error detected: Syntax error
Previous error was cleared by successful operation. using uCalcSoftware; var uc = new uCalc(); // Trigger a syntax error uc.EvalStr("1 +"); // Check the error state after the fact if (uc.Error.Code != ErrorCode.None) { Console.WriteLine($"Error detected: {uc.Error.Message}"); } else { Console.WriteLine("Success!"); } // Perform a successful operation, which clears the error state uc.EvalStr("1 + 1"); if (uc.Error.Code == ErrorCode.None) { Console.WriteLine("Previous error was cleared by successful operation."); }
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// Trigger a syntax error
uc.EvalStr("1 +");
// Check the error state after the fact
if (uc.Error().Code() != ErrorCode::None) {
cout << "Error detected: " << uc.Error().Message() << endl;
} else {
cout << "Success!" << endl;
}
// Perform a successful operation, which clears the error state
uc.EvalStr("1 + 1");
if (uc.Error().Code() == ErrorCode::None) {
cout << "Previous error was cleared by successful operation." << endl;
}
}
Error detected: Syntax error
Previous error was cleared by successful operation. #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // Trigger a syntax error uc.EvalStr("1 +"); // Check the error state after the fact if (uc.Error().Code() != ErrorCode::None) { cout << "Error detected: " << uc.Error().Message() << endl; } else { cout << "Success!" << endl; } // Perform a successful operation, which clears the error state uc.EvalStr("1 + 1"); if (uc.Error().Code() == ErrorCode::None) { cout << "Previous error was cleared by successful operation." << endl; } }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// Trigger a syntax error
uc.EvalStr("1 +")
'// Check the error state after the fact
If uc.Error.Code <> ErrorCode.None Then
Console.WriteLine($"Error detected: {uc.Error.Message}")
Else
Console.WriteLine("Success!")
End If
'// Perform a successful operation, which clears the error state
uc.EvalStr("1 + 1")
If uc.Error.Code = ErrorCode.None Then
Console.WriteLine("Previous error was cleared by successful operation.")
End If
End Sub
End Module
Error detected: Syntax error
Previous error was cleared by successful operation. Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// Trigger a syntax error uc.EvalStr("1 +") '// Check the error state after the fact If uc.Error.Code <> ErrorCode.None Then Console.WriteLine($"Error detected: {uc.Error.Message}") Else Console.WriteLine("Success!") End If '// Perform a successful operation, which clears the error state uc.EvalStr("1 + 1") If uc.Error.Code = ErrorCode.None Then Console.WriteLine("Previous error was cleared by successful operation.") End If End Sub End Module