Throw Throws TryCatch Statements Explanations and Pictures from

















- Slides: 17
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference: https: //docs. oracle. com/javase/tutorial/essential/exceptions/throwing. html
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
Exceptions • As you have probably noticed, the Java platform provides numerous exception classes. • All the classes are descendants of the Throwable class, and 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
Throw Statement • All methods use 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 some. Throwable. Object;
Throw Statement Example private static int sample(int input){ if(input == 0) // Immediately throws exception if 0 throw new Arithmetic. Exception(); return 5 / input; }
Exception Hierarchy
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.
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
Try-Catch Statements Syntax) try{ // Code that may throw an exception } catch(Some. Exception e){ System. out. println(“Exception. Type: “ + e. get. Message()); } ** catch is only executed if the specific exception occurs
Try-Catch Statements try{ // Code that may throw an exception } catch(Some. Exception e){ System. out. println(“Exception. Type: “ + e. get. Message()); } finally{ // Ending statements } ** finally is ALWAYS executed
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; }
Throws Statement • The method below will throw an Arithmetic. Exception when b is equal to 0 • The divide method can notify its callers that an Arithmetic. Exception could be thrown by adding the throws statement below public int divide(int a, int b) throws Arithmetic. Exception{ return a/b; }
Handling Known Exceptions • Although it is not required, the caller of this method can use a try-catch to elegantly handle an Arithmetic. Exception if it occurs public static void main(String[] args) { try { System. out. println(divide(3, 0)); } catch (Arithmetic. Exception e) { System. out. println("Arithmetic Exception: " + e. get. Message()); System. out. println("Second parameter cannot be 0"); } } public static int divide(int a, int b) throws Arithmetic. Exception{ return a/b; }
Common Exceptions • The Exceptions below are testable on the AP Computer Science A Exam 1. Arithmetic. Exception – Dividing by 0 2. Null. Pointer. Exception – Sending a message to a null variable 3. Index. Out. Of. Bounds. Exception – Trying to access index 10 of a list that has 4 elements 4. Array. Index. Out. Of. Bounds. Exception – Trying to access index 11 of 5 element array 5. Illegal. Argument. Exception – occurs when a method is passed an illegal argument
Checked vs. Unchecked Exceptions • Checked Exceptions are exceptions that are subclasses of Exception and not the subclass of Runtime. Exception • Unchecked Exceptions are exceptions that are subclasses of Runtime. Exception • 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 Interrupted. Exception **Interrupted. Exception is a direct subclass of Exception and is therefore a checked Exception. The line above must be placed in a trycatch or a throws statement must be added to the method
Summary • Use throw statement to force an exception to occur • Example) throw new Arithmetic. Exception(); • 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 Arithmetic. Exception{
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