Logging

ERROR HANDLING

https://www.tutorialspoint.com/java/java_exceptions.htm https://docs.oracle.com/javase/8/docs/api/java/lang/Exception.html

Subjects

WHAT ARE ERRORS ?

When the code produces erroneous results

WHY DO ERRORS HAPPEN?

When the code generate errors it can happen for 3 reasons

  1. The programmer made a mistake
  2. The environment changed unexpectedly
  3. The program user has made a mistake

BAD CODING In the first case the programmer has made a mistake: divide by zero index out of bound

ENVIRONMENT PROBLEMS In the second case some physical resource has failed in some way. internet connection was lost
disk error

USER INITIATED ERRORS In the third case the user has made an error
403 forbidden when trying to access a protected resource
Not a number when putting letters in a number field in a form

TYPES OF ERRORS
Syntax errors: Language syntax is not respected Semantic errors: Improper use of program statements Logical errors: Undesired behavior Compile time errors: Syntax errors and static semantic errors indicated by the compiler Runtime errors: Dynamic semantic errors and logical errors, that cannot be detected by the compiler

WHAT TO DO ABOUT ERRORS?
Errors should be handled
User should be notified
Log files should be updated
Program should continue

In Java there are exceptions:

When an exception occur the normal flow of the program is disrupted and the program/application terminates abnormally, which is not recommended, therefore, exceptions should be handled.

UNCHECKED EXCEPTIONS
· Unchecked exceptions occur at run time
· The code will compile even though code throws an exception
· These exceptions mostly comes from bad data from the user
· These exceptions are direct sub classes of RuntimeException class

int result = 2/0; //This will throw an arithmeticException, but it will compile

CHECKED EXCEPTIONS
· Checked exceptions occur at compile time
· These are also called ‘compile time exceptions’.
· These exceptions cannot be ignored at the time of compilation
· The programmer should take care of these exceptions

Netbeans message: “Exception must be caught or declared to be thrown”:
· FileNotFoundException
· IOException
· SQLException

Handle Exceptions

THROWS
If a method does not handle a checked exception, the method must declare it using the throws keyword. The throws keyword appears at the end of a method’s signature.

public Person getPersons() throws ArithmeticException {....}

public Person getPersons() throws MyException {....}

THROW

You can throw an exception, either a newly instantiated one or an exception that you just caught, by using the throw keyword.

throw new ArithmeticException();

throw new MyException();

TRY-CATCH-FINALLY

Catch subclass or superclass
Catch subclass exception
catch (ArithmeticException Excep)
Catch superclass exception
catch (Exception Excep)

Multiple catch types
Different exceptions can be caught in a single catch

catch (NumberFormatException | ArithmeticException Excep){}

Use the different exception methods
· e.printStackTrace()
· e.getMessage()
· e.getStackTrace()[]

ex.printStackTrace();  
System.out.println(Excep.getMessage());  
System.out.println("printStackTrace: " + ex.getStackTrace()[0].toString());    
System.out.println("printStackTraceArrays" + Arrays.toString(ex.getStackTrace()));  

DON´T

CUSTOM EXCEPTIONS
You can create your own exceptions in Java.

All exceptions must be a child of Throwable. class MyException extends Exception {}

An exception class is like any other class and useful fields and methods can be added if needed.

DEMO

Example: How to catch sql exception and throw own exception up through the architectural layers and show it to the user

General logging: Using the java.util.logging package

  1. Setup severity levels ()
  2. Setup behavior
  3. Writing to at log file on linux server

  4. On ubuntu Logging tutorial

  5. LINK til demo project
    Use this command on you linux server to see the your System.out.print statements reported from your web application on tomcat. (NB change if you have a different version of tomcat)

    1. tail -f /var/log/tomcat8/catalina.out
      

      catalina.out is where the System.out is printed on tomcat.

Exercise in class

  1. Use the java.util.logging package to implement a class that can log runtime exceptions to the console.
  2. Extend the logger to also write to a file.
  3. Create a program flow where sql exceptions are both logged to a file on the server and also generates an error message that is send to the user.
  4. Test it on a deployed webapp on your droplet.