Skip to the content.

Exceptions, Assertions, Logging

-

What we’ll cover

Throwable Class

Basic Exception Handling

Better Exception Handling

Finally Keyword

Assertions

Logging

-

….a brief introduction:

Programs can fail for various reasons:

Some of these issues are coding errors, while others are beyond the coder’s control. However, anticipating such errors and handling them appropriately is crucial.

-

Throwable Class

-

Throwable Hierarchy

from techfinanceworld

-

Errors

The Error hierarchy describes internal errors and resource exhaustion situations.

-

Unchecked Exceptions

-

Checked Exceptions

-

Throwing Exceptions

Here are a couple of ways an exception can be thrown:

-

Basic Exception Handling

-

What is Exception Handling?

-

Unchecked Unhandled Exception Example; Compile Error

import java.io.*;
class FilePrinter {
    private final BufferedReader reader;

    public FilePrinter(String fileDirectory) {
		// What if the file does not exist?
		this.reader = new BufferedReader(new FileReader(fileDirectory));
    }

    public void printFile() {
        String line = null;
        do {
            // What if the System fails to read in the next line?
            // (For example if the file was suddenly closed, modified, or deleted)
            line = reader.readLine();
            System.out.println(line);
        } while (line != null);
    }
}

-

Exception Handling; Signature Throw Clause

import java.io.*;
class FilePrinter {
    private final BufferedReader reader;

    public FilePrinter(String fileDirectory) throws FileNotFoundException {
        this.reader = new BufferedReader(new FileReader(fileDirectory));
    }

    public void printFile() throws IOException {
        String line = null;
        do {
            line = reader.readLine();
            System.out.println(line);
        } while (line != null);
    }
}

-

Exception Handling; Try / Catch

import java.io.*;
class FilePrinter {
    private final BufferedReader reader;

    public FilePrinter(String fileDirectory) throws FileNotFoundException {
        this.reader = new BufferedReader(new FileReader(fileDirectory));
    }

    public void printFile() throws IOException {
        String line = null;
        do {
            line = reader.readLine();
            System.out.println(line);
        } while (line != null);
    }

    public void tryPrintFile() {
        try {
            printFile();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

-

Better Exception Handling

-

Multi-Exception Handling

-

Multi-Exception Handling Examples

public class FilePrinterTest {
    private static final String invalidFileName = "";

    @Test(expected = FileNotFoundException.class)
    public void testInstantiation() throws FileNotFoundException {
        FilePrinter fpt = new FilePrinter(invalidFileName);
    }

    // Attempt to instantiate FilePrinter with invalid name
    // Attempt to invoke method on unininstatiated FilePrinter object
    @Test(expected = NullPointerException.class)
    public void testNullPointer() throws NullPointerException {
        FilePrinter fpt = null;
        try {
            fpt = new FilePrinter(invalidFileName);
        } catch (FileNotFoundException e) {
            System.out.println("Printing stack trace...");
            e.printStackTrace();
        }
        fpt.tryPrintFile();
    }

    @Test(expected = NullPointerException.class)
    public void testMultiThrowSignature() throws NullPointerException, FileNotFoundException {
        testNullPointer();
        testInstantiation();
    }

}

-

Dynamic Exception Handling; Expanded

public class FilePrinterTest {
	private static final String invalidFileName = "";
	public void testInstantiateAndPrint() {
	    FilePrinter fpt = null;
	    try {
	        fpt = new FilePrinter(invalidFileName);
	    } catch(FileNotFoundException fnfe) {
	        fnfe.printStackTrace();
	    }

	    try {
	        fpt.printFile();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	}
}

-

Dynamic Exception Handling; Compressed

public class FilePrinterTest {
	private static final String invalidFileName = "";
	public void testInstantiateAndPrint() {
	    FilePrinter fpt = null;
	    try {
	        fpt = new FilePrinter(invalidFileName);
	        fpt.printFile();
	    } catch(FileNotFoundException fnfe) {
	        // handle FileNotFoundException
	        fnfe.printStackTrace();
	    } catch(IOEXception ioe) {
	        // handle IOException	    
	        ioe.printStackTrace();
	    }
	}
}

-

Uniform Handling Of Exceptions (Good)

public class FilePrinterTest {
	private static final String invalidFileName = "";
	public void testInstantiateAndPrint() {
	    FilePrinter fpt = null;
	    try {
	        fpt = new FilePrinter(invalidFileName);
	        fpt.printFile();

	    // bit-wise operator supported by java 1.7+    
	    } catch(FileNotFoundException | IOException exception) {
	        // handle all exceptions the same way
	        exception.printStackTrace();
	    }
	}

}

-

Uniform Handling Of Exceptions (Bad)

public class FilePrinterTest {
	private static final String invalidFileName = "";
	public void testInstantiateAndPrint() {
	    FilePrinter fpt = null;
	    try {
	        fpt = new FilePrinter(invalidFileName);
	        fpt.printFile();
	    } catch(Exception exception) {
	        // handle all exceptions the same way
	        exception.printStackTrace();
        } catch(IllegalArgumentException iae) {
            iae.printStackTrace();
        }
    }

    public void parseIntegerInput(String s) {
        try {
            Long.parseLong(s);
        } catch(NumberFormatException e) {
            e.printStackTrace();
            throw new IllegalArgumentException();
        }
    }
}

-

Recursion and Exception Handling

-

Finally Keyword

-

Purpose

-

Conditions under which finally block is executed

  1. If no exception are thrown.
  2. If exception outside try block is thrown.
  3. If an exception is thrown in a catch clause.
  4. The program skips to the finally clause, if the catch clause does not throw an exception.

-

Decoupling finally clause from try/catch clauses

class BookExample {
	public void example1() {
		InputStream in = ... ;
		try {
			try {
				// code that may throw exception
			} finally {
				in.close();
			}
		} catch(IOException ioe) {
			/// handle exception some way
		}
	}
}

-

Exceptions and Inheritance

When a method overrides a method from a superclass or interface, it is not allowed to add checked exceptions. It is, however, allowed to declare fewer exceptions or declare a subclass of a declared exception. Methods declare exceptions with the keyword throws.

-

Exceptions and Inheritance (continued)

Adding a checked exception to a method won’t compile:

class HasNoMovesException extends Exception { } 
class Dancer {
    public void dance() { }
 }
class Boogie extends Dancer {
    public void dance() throws HasNoMovesException { 
     // DOES NOT COMPILE
    }
}

-

Exceptions and Inheritance (continued)

…declaring fewer exceptions, or declaring a subclass of the exception type are perfectly legal, however.

class Dancer {
    public void hop() throws HasNoMovesException { }
}
class Boogie extends Dancer {
    public void dance() { } 
}

class Dancer {
    public void dance() throws Exception { }
}
class Boogie extends Dancer {
    public void dance() throws HasNoMovesException { } 
}

-

You could also throw it explicitly:


public void dance() {
    throw new RuntimeException("I have no moves!");
}

-

Assertions

-

Toggling Assert Statements

-

When To Use

-

Logging

-

Principal advantages of Logging API

-

The 7 Logging Levels

-

Syntax

public class LogDemo {
	 // it is advised that you name your logger the same as your Main Application package
    Logger logger = Logger.getLogger("com.zipcodewilmington.MainApplication");
    public void logTest() {
        logger.setLevel(Level.SEVERE);  // log severe
        logger.setLevel(Level.WARNING); // log severe, warning
        logger.setLevel(Level.INFO);    // log severe, warning, info
        logger.setLevel(Level.CONFIG);  // log severe, warning, info, config
        logger.setLevel(Level.FINE);    // log severe, warning, info, config, fine
        logger.setLevel(Level.FINER);   // log severe, warning, info, config, fine, finer
        logger.setLevel(Level.FINEST);  // log severe, warning, info, config, fine, finer, finest
    }
}

-