How to Log Errors & Catch Them Through Exceptions in Java

Java includes a library that you use for error handling and logging the errors to a text file. You use error handling in case an error occurs in your application. Instead of crashing, the error is "caught" in the error handler function. You can use the Java handler events to catch the errors and the Java file handlers to write the error to a file.

Instructions

    • 1

      Open the Eclipse Java environment on your computer and open the project that needs error handling. Double-click the Java source code file you want to use.

    • 2

      Create the error handler function. The following code creates a function for Java errors:

      public static void main(String args[]) throws Exception{
      }

    • 3

      Set up the file to write the error to the text file. The following code uses the "errors.txt" file to write errors to a log:

      FileHandler file= new FileHandler("errors.txt");

    • 4

      Write the error to the file. The "Error" class contains the errors trapped in your program. The following code uses the errors.txt file to log errors:

      Logger lpg = Logger.getLogger("");
      try {

      } catch (Error ex) {
      log.log(Level.INFO, "", ex);
      }

Related Searches:

References

Comments

Related Ads

Featured