Although not specifically specified in the language, if the try-catch
encounters an error, you want to save the error as a log (WriteExceptionToLogFile
section of the code below).
try
{
// Source from which the exception occurs
}
catch (Exception exception)
{
// Write error to log file
WriteExceptionToLogFile (exception);
// Show Errors
DisplayErrorMessageBox("Error Occurred!");
}
Up until now, I have used the above code, but considering that WriteExceptionToLogFile
causes errors, I have noticed that there are two problems.
WriteExceptionToLogFile
causes an error where the program stopsWriteExceptionToLogFile
In addition to not knowing its own errors, you will not know the exceptions that were supposed to be written by WriteExceptionToLogFile
Is there a way to resolve the above two issues when a logger running in the catch
section stops due to an error, or is there an implicit understanding that the logger does not generate an error in the first place?
WriteExceptionToLogFile
causes an error, where the program stops
I think the word "error" means "Exception", but if an exception is likely to be thrown within WriteExceptionToLogFile
, catch it and continue to process it, the program will not stop.
WriteExceptionToLogFile
I don't know my own error, and I don't know the exceptions that were supposed to be written by WriteExceptionToLogFile
You can catch an exception thrown in WriteExceptionToLogFile
and print the exception and the original exception to another file.However, if writing to the log file fails, it would be better to output it to something other than the log file, such as the standard (error) output, because it is a very exception case such as insufficient disk space.
try{
// Source from which the exception occurs
} catch(Exception exception) {
// Write error to log file
WriteExceptionToLogFile (exception);
// Show Errors
DisplayErrorMessageBox("Error Occurred!");
}
}
private void WriteExceptionToLogFile (Exception exception) {
try{
// Write error to log file
} catch(Exceptione){
// Output to standard error stream
e.printStackTrace();
System.err.println("Original Exception:");
exception.printStackTrace();
}
}
© 2024 OneMinuteCode. All rights reserved.