Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.

Similar presentations


Presentation on theme: "Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this."— Presentation transcript:

1 Exceptions and Error Handling

2 Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this

3 When an error occurs You can deal with it, or you can ‘pass the buck’ and let the caller handle the exception

4 Try-Catch Blocks try { // some stuff that may cause an error } catch (the first error type) { // do something about this specific error type } catch (another error type) { // do something about this second error type }

5 Try-Catch Blocks A statement in a try { } block throws an exception! Let’s divide by zero Control immediately passes to the catch block for the type of exception thrown The catch code executes, and then control goes to the code after the last catch block try appropriate catch code after all catches

6 Exception Handling When an exception occurs… If it is not handled by the method… It goes to the caller. If it’s not handled by the method… It goes to the caller …and so on until it gets to main

7 Example class ExceptionExample { private int[] myArray; public ExceptionExample() { myArray = new int[10]; } public void causeTrouble() { reallyCauseTrouble(); } public void reallyCauseTrouble() { myArray[10] = 88; } public class TestException { public static void main (String [] args) { ExceptionExample e1 = new ExceptionExample(); e1.causeTrouble(); } Oops! Array out of bounds… but I don’t handle exceptions! Oops! Array out of bounds… but I don’t handle exceptions!

8 Example class ExceptionExample { private int[] myArray; public ExceptionExample() { myArray = new int[10]; } public void causeTrouble() { reallyCauseTrouble(); } public void reallyCauseTrouble() { myArray[10] = 88; } public class TestException { public static void main (String [] args) { ExceptionExample e1 = new ExceptionExample(); e1.causeTrouble(); } So return to my caller and see if they handle exceptions (have a catch block)… not here either

9 Example class ExceptionExample { private int[] myArray; public ExceptionExample() { myArray = new int[10]; } public void causeTrouble() { reallyCauseTrouble(); } public void reallyCauseTrouble() { myArray[10] = 88; } public class TestException { public static void main (String [] args) { ExceptionExample e1 = new ExceptionExample(); e1.causeTrouble(); } So return to my caller and see if they handle exceptions (have a catch block)… not here either

10 Example class ExceptionExample { private int[] myArray; public ExceptionExample() { myArray = new int[10]; } public void causeTrouble() { reallyCauseTrouble(); } public void reallyCauseTrouble() { myArray[10] = 88; } public class TestException { public static void main (String [] args) { ExceptionExample e1 = new ExceptionExample(); e1.causeTrouble(); } But this is the main method, so the program terminates with an ugly error message.

11 Example class ExceptionExample { private int[] myArray; public ExceptionExample() { myArray = new int[10]; } public void causeTrouble() { reallyCauseTrouble(); } public void reallyCauseTrouble() { myArray[10] = 88; } public class TestException { public static void main (String [] args) { ExceptionExample e1 = new ExceptionExample(); e1.causeTrouble(); } stack trace: Java.lang.ArrayIndexOutOfBoundsException: 10 at ExceptionExample.reallyCauseTrouble( ) at EceptionExample.causeTrouble( ) at TestException.main( ) stack trace: Java.lang.ArrayIndexOutOfBoundsException: 10 at ExceptionExample.reallyCauseTrouble( ) at EceptionExample.causeTrouble( ) at TestException.main( )

12 Example class ExceptionExample { private int[] myArray; public void reallyCauseTrouble() { try { myArray[10] = 88; } catch (ArrayIndexOutOfBoundsException e) { System.out.println (“The element you requested is not available”); } But this may not be the best place to handle the error – at a higher level, we can write fewer catch blocks… maybe we move it up one level to causeTrouble( ).

13 How do I know the available exceptions? Some common ones: InputMismatchException NullPointerException ArrayIndexOutOfBoundsException ArithmeticException FileNotFoundException Consult reference for others – or you can create your own (later)

14 Checked Exceptions Can be caught by the compiler Ex: FileNotFoundException You must deal with them, or declare that your method ‘throws’ that exception (passes the buck).

15 How does the compiler know How do we distinguish checked exceptions?

16 How does the compiler know Sneaky… The constructor to certain objects will throw an Exception. If you make one of these objects, you need to catch it or throw it to your caller.

17 Unchecked Exceptions Things the compiler cannot know about. A variable is created, user sets it to zero, then divides by it. We can ‘catch’ them, or it is left to Java’s default exception handler, and you get the ugly stack trace message

18 I don’t want to catch it, I want to throw… public static void myMethod ( ) throws IOException, NumberFormatException { // if you call me, you need to catch those exceptions, or throw them again }

19 This is a pain This can be a lot of code, to catch every possible error in user input. You should do it anyway Commercial code is as much error checking as ‘real’ code.

20 Example import java.util.*; public class ExceptionExample { Scanner console = new Scanner(System.in); public static void main(String[] args) { try { System.out.println(“Enter 2 Numbers”); int quotient = console.nextInt() / console.nextInt(); } catch (Exception e) { if (e instanceof ArithmeticExcption) { System.out.println(“div by 0”); } else if (e instanceof InputMismatchException) { System.out.println(“Input Mismatch in Types” + e.toString() ); } } // end catch

21 throw statement throw new InputMismatchException(“my message”); This has the caller deal with it Note: we use another object with no name We pass a message string to the InputMismatchException constructor.

22 I want to do more than just display a message do { Scanner console = new Scanner(System.in); try { number = console.nextInt(); done = true; } catch (InputMismatchException e) { // give error message } }while (!done);

23 Just extend ‘Exception’ Then throw your new exception type and catch it. Making Your Own Exception Class


Download ppt "Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this."

Similar presentations


Ads by Google