Studyon Minte9.com
Fundamentals (Docs)

Study

Exceptions



Definition

The term exception is shorthand for the phrase "exceptional event".
Creating an exception object and handing it to the runtime system is called throwing an exception.

	import java.io.*;
	public class ExceptionsDemo {
	    public static void main(String[] args) {
		new ExceptionsDemo().go();
	    }
	    public void go() {
		try {
		    PrintWriter out = new PrintWriter(new FileWriter("outfile.txt"));
			// won't compile withou try/catch

		    out.println("Output text");
		    out.close();
		} catch (IOException ex) {}
	    }
	}

Finally

The finally block always executes when the try block exits. This ensures that the finally block is executed even if 
an unexpected exception occurs. Also it allows the programmer to avoid having cleanup code accidentally 
bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, 
even when no exceptions are anticipated.

	import java.io.*;
	public class ExceptionsDemo2Finally {
	    public static void main(String[] args) {
		new ExceptionsDemo2Finally().go();
	    }
	    public void go() {
		try {
		    // outfile.txt made read-only
		    // try block failed

		    PrintWriter out = new PrintWriter(new FileWriter("outfile.txt"));
		    out.println("Output text");
		    out.close();
		} catch (IOException ex) {
			//ex.printStackTrace();
		    System.out.print("1 ");
		} finally {
		    System.out.print("2");
		}
		
		// output: 1 2
	    }
	}

Recover resources

The finally block is a key tool for preventing resource leaks. When closing a file or otherwise recovering 
resources, place the code in a finally block to ensure that resource is always recovered.

	try {
	    PrintWriter out = new PrintWriter(new FileWriter("outfile.txt"));
	    out.println("Output text");
	} catch (IOException ex) {
	    System.out.print("1 ");
	} finally {
	    System.out.print("2");
	    out.close();
	}

Throws statement

Method go() throws IOException

	import java.io.*;
	public class ExceptionsDemo3Throws {
	    public static void main(String[] args) {
		try {
		    new ExceptionsDemo3Throws().go();
		} catch (IOException ex) {}
	    }
	    public void go() throws IOException {
		out = new PrintWriter(new FileWriter("outfile.txt"));
		out.println("Output text");
		out.close();
	    }
	}


Try-with-resources

The try-with-resources statement is a try statement that declares one or more resources.

	import java.io.*;
	public class ExceptionsDemo4Resources {
	    public static void main(String[] args) {
		new ExceptionsDemo4Resources().go();   
	    }
	    public void go() {
		try {
		    String out = readFirstLineFromFile("outfile.txt"); 
		} catch (IOException ex) {}
	    }
	    
	    static String readFirstLineFromFile (String path) throws IOException  {
		
		// the following example reads the first line from a file

		try (BufferedReader br = new BufferedReader(new FileReader(path))) {
		    return br.readLine();
		}
	    }
	}


http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html