exceptions and errors in java

32
Exceptions and Errors in Java Manuela Grindei Software Developer at Gamesys [email protected]

Upload: manuela-grindei

Post on 15-Apr-2017

212 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Exceptions and errors in Java

Exceptions and Errors in Java

Manuela GrindeiSoftware Developer at Gamesys

[email protected]

Page 2: Exceptions and errors in Java

Agenda• Examples• Class Hierarchy• try-catch-finally blocks• multi-catch and try-with-resources• Exception propagation• Checked vs unchecked exceptions• Custom exceptions

Page 3: Exceptions and errors in Java

Example 1What will be the outcome of the following program and why?

int[] a = {1,2,3,4,5};for (int i = 1; i <= 5 ; i++) { System.out.println(a[i]);}

System.out.println(“Finished”);

Page 4: Exceptions and errors in Java

Example 1What will be the outcome of the following program and why?

int[] a = {1,2,3,4,5};for (int i = 1; i <= 5 ; i++) { System.out.println(a[i]);}

System.out.println(“Finished”);

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5

Page 5: Exceptions and errors in Java

Example 2What will be the outcome of the following program and why?

static String s;public static void main(String[] args) { System.out.println(s.length());}

Page 6: Exceptions and errors in Java

Example 2What will be the outcome of the following program and why?

static String s;public static void main(String[] args) { System.out.println(s.length());}

Exception in thread "main" java.lang.NullPointerException

Page 7: Exceptions and errors in Java

Example 3What will be the outcome of the following program and why?

Integer i = 2;Long l = (Long)(Number) i;System.out.println(l);

Page 8: Exceptions and errors in Java

Example 3What will be the outcome of the following program and why?

Integer i = 2;Long l = (Long)(Number) i;System.out.println(l);

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

Page 9: Exceptions and errors in Java

Example 4Fibonacci Numbers

Fib0 = 1Fib1 = 1Fibn = Fibn-1 + Fibn-2

long fib(long i) { if (i == 0) { return 1; } if (i == 1) { return 1; } else { return fib(i - 1) + fib(i - 2); }}

fib(3) = ?fib(100_000) = ?

Page 10: Exceptions and errors in Java

Example 4Fibonacci Numbers

Fib0 = 1Fib1 = 1Fibn = Fibn-1 + Fibn-2

long fib(long i) { if (i == 0) { return 1; } if (i == 1) { return 1; } else { return fib(i - 1) + fib(i - 2); }}

fib(3) – returns correct answerfib(100_000) - StackOverflowError

Page 11: Exceptions and errors in Java

Class Hierarchy

serious problems that a reasonable application should not try to catch

Checked exceptions

Unchecked exceptions

conditions that a reasonable application might want to catch

Page 12: Exceptions and errors in Java

Exceptions in Java 8

Page 13: Exceptions and errors in Java

Runtime Exceptions is Java 8

Page 14: Exceptions and errors in Java

Errors in Java 8

Page 15: Exceptions and errors in Java

Exceptions• Exceptions are thrown by a program, and may be caught and handled by

another part of the program

• A program can have a normal execution flow and an exception execution flow

• Java has a predefined set of exceptions and errors that can occur during execution

• A program can deal with an exception by:• ignoring it (see first examples)• handling it where it occurs• handling it in another place in the program

Page 16: Exceptions and errors in Java

try-catchtry {//guarded region, i.e. code that might throw exceptions…} catch (Exception1 e1) {//Exception1 handler…} catch (Exception2 e2) {//Exception2 handler…}

- If an exception occurs in try block, the control will get transferred to the appropriate catch handler

- A catch handler shows that we know how to handle exceptions of that type

- If Exception1 is a subclass of Exception2, its catch block should come first (start with the most specific exception)

Page 17: Exceptions and errors in Java

Exception swallowingDo NOT write catch handlers like this:catch(Exception e) {}- program continues processing as if nothing had gone wrong - the ignored exception may lead the application to an unexpected failure- code can be hard to debug- if the exception really needs to be caught, log some information about it!

Page 18: Exceptions and errors in Java

ExampleFile file = new File("file.txt");Writer writer = null;try { writer = new PrintWriter(file); writer.write(new BigDecimal("Hello world!").toString()); writer.flush(); writer.close();} catch(FileNotFoundException e) { System.out.println("Caught " + e.getClass().getName());} catch (IOException e) { System.out.println("Caught IOException: " + e.getMessage());} catch (NumberFormatException e) { System.out.println("Caught " + e.getClass().getName());}

What is wrong with the above code?

Page 19: Exceptions and errors in Java

ExampleFile file = new File("file.txt");Writer writer = null;try { writer = new PrintWriter(file); writer.write(new BigDecimal("Hello world!").toString()); writer.flush(); writer.close();} catch(FileNotFoundException e) { System.out.println("Caught " + e.getClass().getName());} catch (IOException e) { System.out.println("Caught IOException: " + e.getMessage());} catch (NumberFormatException e) { System.out.println("Caught " + e.getClass().getName());}

Is the file always closed?

Page 20: Exceptions and errors in Java

FixFile file = new File("file.txt");Writer writer = null;try { writer = new PrintWriter(file); writer.write(new BigDecimal("Hello world!").toString()); writer.flush();}catch (NumberFormatException e) { System.out.println("Caught " + e.getClass().getName());}catch(FileNotFoundException e) { System.out.println("Caught " + e.getClass().getName());}catch (IOException e) { System.out.println("Caught IOException "+ e.getMessage());} finally { if (writer != null) try { writer.close(); } catch (IOException e) { System.err.println("Caught IOException"); }}

Page 21: Exceptions and errors in Java

Finally block• any try block needs to be followed by at least a catch or finally block• finally block always gets executed (unless JVM exits while the try/catch is

being executed or the thread executing the try/catch is interrupted or killed)• if there is a return statement in the try block, the finally block executes right

after the return statement is encountered, and before the return executes• if an exception is thrown, finally block executes immediately after the

corresponding catch block completes• if there is no exception, finally executes immediately after the try block• finally is the ideal place to release resources

Page 22: Exceptions and errors in Java

Multi-catchFile file = new File("file.txt");Writer writer = null;try { writer = new PrintWriter(file); writer.write(new BigDecimal("Hello world!").toString()); writer.flush();}catch (NumberFormatException | IOException e) { //e is final//the exceptions must be in different inheritance hierarchies System.out.println("Caught " + e.getClass().getName());} finally { if (writer != null) try { writer.close(); } catch (IOException e) { System.out.println("Caught IOException: " + e.getMessage()); }}

Page 23: Exceptions and errors in Java

try-with-resourcespublic static void main(String[] args) throws IOException {File file = new File("file.txt");try(Writer writer = new PrintWriter(file)) { writer.write(new BigDecimal("Hello world!").toString()); writer.flush();}}

- Code is more concise and readable- Automatic Resource Management closes the file for us- The resources are declared in try between () and must implement

AutoCloseable

Page 24: Exceptions and errors in Java

throw/throws• A method can throw a new exception: throw new Exception();throw new IllegalArgumentException();

• A method can specify in its declaration that it throws one or more exceptionsvoid f() throws Exception1, Exception2; void f() throws Exception1, Exception2{//…}

Page 25: Exceptions and errors in Java

Exception propagationpublic static void main(String[] args) { doStuff();}private static void doStuff() { doMoreStuff();}private static void doMoreStuff() { int x = 5/0;}

Exception in thread "main" java.lang.ArithmeticException: / by zeroat Exercise.doMoreStuff(Exercise.java:10)at Exercise.doStuff(Exercise.java:7)at Exercise.main(Exercise.java:4)

Page 26: Exceptions and errors in Java

Checked vs unchecked exceptionsChecked Exceptions Unchecked Exceptions

Checked by the compiler Unchecked by the compiler

Extend Exception Extend RuntimeException

If a method throws them, they need to be specified in that method’s declaration

If a method throws them, they can be specified in the declaration, but it’s not mandatory

They need to either be caught or rethrown explicitly by a method

No restrictions

Show recoverable conditions Show programming errors

Page 27: Exceptions and errors in Java

Custom Exceptions• We can create our own business exceptions by extending either

Exception or RuntimeException• We can use our exceptions just like the standard ones

Page 28: Exceptions and errors in Java

Example - PersonValidatorpublic class Person { private String name; private int age;

public Person() {}

public Person(String name, int age) { this.name = name; this.age = age; } //getters and setters}

public class PersonValidationException extends RuntimeException { public PersonValidationException(String s) { super(s); }}

}

Page 29: Exceptions and errors in Java

Example - PersonValidatorpublic final class PersonValidator { public static final int MAX_LENGTH = 60; private PersonValidator(){}

public static void validate(Person p) { if (p.getName() == null || p.getName().isEmpty()) { throw new PersonValidationException("The person must have a name"); } if (p.getName().length() > MAX_LENGTH) { throw new PersonValidationException("The name cannot be longer than " + MAX_LENGTH + " characters"); } if (p.getAge() < 18) { throw new PersonValidationException("The person cannot be underage"); } }}

Page 30: Exceptions and errors in Java

Example - PersonValidatorpublic static void main(String[] args) { Person validPerson = new Person("John", 25); PersonValidator.validate(validPerson);

Person personWithInvalidAge = new Person("Tom", 17); PersonValidator.validate(personWithInvalidAge);}

Page 31: Exceptions and errors in Java

Benefits of exception handling• detect errors easily without writing additional code to test return

values• exception-handling code is clearly separated from exception-

generating code• the same exception-handling code can deal with several possible

exceptions• code to handle an exception that may occur in the governed region

needs to be written only once

Page 32: Exceptions and errors in Java

Conclusions• Do NOT swallow exceptions• Use checked exceptions for conditions from which the caller can

reasonably be expected to recover the exceptional condition cannot be prevented by proper use of the API the programmer using the API can take some useful action once confronted

with the exception

• Use runtime exceptions to indicate programming errors (mostly precondition violations)

• By convention: errors are reserved for use by the JVM to indicate resource deficiencies, invariant failures etc. – do NOT subclass Error