Ocp Exceptions

try with resources (don’t need catch or finally, as implicit finally)

try(openResource file){}

try (normal) MUST have a catch or a finally

try{}

A Catch can’t ever be empty e.g.

catch(SomeException e){}

catch {}

 

Hierarchy (this is not all the exceptions, just a subset to visualise )

All Exceptions that inherit from Exception (but don’t inherit from RuntimeException), are checked Exceptions.

Inheritance Related Exceptions – careful !

		// does NOT compile ( IOException catches all FileNotFoundException exceptions )
		// unreachable code 
		// FileNotFoundException is-a IOException
		try {
			throw new FileNotFoundException();
		}
		catch(IOException io) {}
		catch(FileNotFoundException fn) {}
		
		// compiles(more specific (related) exception first)
		try {
			throw new FileNotFoundException();
		}
		catch(FileNotFoundException io) {}
		catch(IOException fn) {}
		
		// does NOT compile either way around (only one of these valid here)
		try {
			// code			
		}catch( FileNotFoundException  | IOException  e) {}

 

Finally in Exceptions