Presentation is loading. Please wait.

Presentation is loading. Please wait.

Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference: https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html.

Similar presentations


Presentation on theme: "Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference: https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html."— Presentation transcript:

1 Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference: https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

2 Throw Statements Before you can catch an exception, some code somewhere must throw one. Any code can throw an exception: your code, code from a package written by someone else such as the packages that come with the Java platform, or the Java runtime environment. Regardless of what throws the exception, it's always thrown with the throw statement. Reference: https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

3 Exceptions As you have probably noticed, the Java platform provides numerous exception classes. All the classes are descendants of the Throwable class, and all allow programs to differentiate among the various types of exceptions that can occur during the execution of a program. You can also create your own exception classes to represent problems that can occur within the classes you write. In fact, if you are a package developer, you might have to create your own set of exception classes to allow users to differentiate an error that can occur in your package from errors that occur in the Java platform or other packages. Reference: https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

4 Throw Statement All methods use the throw statement to throw an exception. The throw statement requires a single argument: a Throwable object. Throwable objects are instances of any subclass of the Throwable class. Syntax: throw someThrowableObject;

5 Throw Statement Example private static int sample(int input){ if(input == 0) // Immediately throws exception if 0 throw new ArithmeticException(); return 5 / input; }

6 Exception Hierarchy

7 Exceptions vs. Errors Error Class When a dynamic linking failure or other hard failure in the Java virtual machine occurs, the virtual machine throws an Error. Exception Class Most programs throw and catch objects that derive from the Exception class. An Exception indicates that a problem occurred, but it is not a serious system problem. Most programs you write will throw and catch Exceptions as opposed to Errors.

8 Try-Catch Statements Valid Java programming language code must honor the Catch or Specify Requirement. This means that code that might throw certain exceptions must be enclosed by either of the following: A try statement that catches the appropriate exception A method that specifies that it can throw a particular type of exception

9 Try-Catch Statements Syntax) try{ // Code that may throw an exception } catch(SomeException e){ System.out.println(“ExceptionType: “ + e.getMessage()); } ** catch is only executed if the specific exception occurs

10 Try-Catch Statements try{ // Code that may throw an exception } catch(SomeException e){ System.out.println(“ExceptionType: “ + e.getMessage()); } finally{ // Ending statements } ** finally is ALWAYS executed

11 Throws Statement Sometimes it may be a better choice to notify other methods that an exception may occur rather than handling the method with a try-catch A throws statement is used to show what type of an exception a method may throw First, look at the method below: public int divide(int a, int b){ return a/b; }

12 Throws Statement The method below will throw an ArithmeticException when b is equal to 0 The divide method can notify its callers that an ArithmeticException could be thrown by adding the throws statement below public int divide(int a, int b) throws ArithmeticException{ return a/b; }

13 Handling Known Exceptions Although it is not required, the caller of this method can use a try-catch to elegantly handle an ArithmeticException if it occurs public static void main(String[] args) { try { System.out.println(divide(3, 0)); } catch (ArithmeticException e) { System.out.println("Arithmetic Exception: " + e.getMessage()); System.out.println("Second parameter cannot be 0"); } public static int divide(int a, int b) throws ArithmeticException{ return a/b; }

14 Common Exceptions The Exceptions below are testable on the AP Computer Science A Exam 1.ArithmeticException – Dividing by 0 2.NullPointerException – Sending a message to a null variable 3.IndexOutOfBoundsException – Trying to access index 10 of a list that has 4 elements 4.ArrayIndexOutOfBoundsException – Trying to access index 11 of 5 element array 5.IllegalArgumentException – occurs when a method is passed an illegal argument

15 Checked vs. Unchecked Exceptions Checked Exceptions are exceptions that are subclasses of Exception and not the subclass of RuntimeException Unchecked Exceptions are exceptions that are subclasses of RuntimeException Java requires all checked exceptions to be “handled” (caught or specified) Java does not require unchecked exceptions to be handled by the programmer Example) Thread.sleep(1000); //Throws InterruptedException **InterruptedException is a direct subclass of Exception and is therefore a checked Exception. The line above must be placed in a try- catch or a throws statement must be added to the method

16 Summary Use the throw statement to force an exception to occur Example) throw new ArithmeticException(); If it is known that an error may occur, you must catch or specify Catch an Exception with a try-catch statement try{ } catch(Exception e){ } Specify that an Exception occurs by adding a throws statement next to the method header Example) public int mystery(int a, int b) throws ArithmeticException{

17 Types of Errors Review Logic – An error that occurs when the program runs but unexpected results are produced Syntax – An error in spelling, punctuation, or placement of certain key symbols in a program Runtime – An error detected after compilation – results in an error message being produced rather than the expected output


Download ppt "Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference: https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html."

Similar presentations


Ads by Google