Throw
You might have a method where you think certain things should be handled (like if you run into a floating point error and want to handle that case explicitly), but Java will not automatically throw an exception because it does not see an issue.
If you want to explicitly invoke an exception from a method (to let a different part of your program know that something went wrong), you use the throw keyword and create a new instance of the Exception subclass that represents the type of exception you are dealing with. [common subclasses]
public static void main(String[] args) { try { System.out.println(divide(7, 2)); } catch (ArithmeticException e) { System.out.println("There was an arithmetic exception."); } } public static int divide(int a, int b) { int result = a / b; if (result != (double) a / (double) b) throw new ArithmeticException(); System.out.println("Got to end of divide()"); return a / b; }
In the code above, if the integer division of a and b does not equal the exact value of a / b, the divide() method throws an ArithmeticException. This allows the main() method to handle the exception, instead of having to handle it in the divide() method.
Checked and Unchecked Exceptions
What happens when you don’t bother with exception handling?
In the above case, just like how you encounter runtime errors if you divide by zero, neglecting to handle the ArithmeticException would not give you a compiler error, since ArithmeticException is an unchecked exception. You would simply get a runtime error if the exception actually occurred.
In Java, there are checked and unchecked exceptions.
Unchecked exceptions, like ArithmeticException and ArrayIndexOutOfBoundsException, are exceptions that are handled at runtime. Java does not require these to be directly handled using try/catch block, so if an unchecked exception occurs and is not handled, this will cause a RuntimeException because the program will stop running.
(Note: ArithmeticException and ArrayIndexOutOfBoundsException are two subclasses of the Exception class, which is why they are written as one word.)
Checked exceptions are checked at compile time because if they are not handled directly (with try/catch) or declared to be thrown with the throws keyword, you will get a compile error. These exceptions will never result in a runtime exception (unless the programmer specifically handles them by making the program exit) because they must be dealt with in the code.
In other words, throws is used to postpone the error handling.
Example:
public static void access(File file) { if(file == null) throw new IOException(); }
In the example above, an IOException is one of Java’s checked exceptions (you will learn more about IOExceptions in the file handling sections later in the chapter). This code results in the following compiler error because checked exceptions must be handled, and the above code does not handle the exception with a try/catch block:Throws.
Throws.java:24: error: unreported exception IOException: must be caught or declared to be thrown throw new IOException(); ^
If we want to handle the IOException in the main class, we need to use the throws keyword to declare the possible exception, and then we can handle it in the main class:
public static void main(String[] args) { try { File file = null; access(file); } catch (IOException e) { System.out.println("There was an IOException."); } } public static void access(File file) throws IOException { if (file == null) throw new IOException(); }
Subclasses of Exception
You can declare any superclasses of an Exception subclass to be thrown instead. For example, instead of “throws IOException”, we could have written “throws Exception” since IOException is a subclass of Exception, and is therefore included as a possibility in the exception declaration.
However, this means you’ll have to catch the superclass exception. If you throw Exception, you must catch Exception (instead of only IOException) in the try/catch blocks where you are doing the exception handling. But if you throw IOException, then you can catch either IOException or Exception (since IOException is included under this superclass).
Copyright © 2021 Code 4 Tomorrow. All rights reserved.
The code in this course is licensed under the MIT License.
If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.