One declares it, and the other one does it 🙂
There are five keywords related to Exception handling in Java e.g. try, catch, finally, throw and throws. Apart from difference between final, finally and finalize, throw vs throws is one of the frequently asked Java interview question.
- throw keyword is used to throw Exception from any method or static block in Java while throws keyword, used in method declaration, denoted which Exception can possible be thrown by this method. They are not interchangeable.
- If any method throws checked Exception as shown in below Example, than caller can either handle this exception by catching it or can re throw it by declaring another throws clause in method declaration.
- The Throw clause can be used in any part of code where you feel a specific exception needs to be thrown to the calling method.
Understating Java Exceptions:
java.lang.Object | +--java.lang.Throwable | +--java.lang.Exception | | | +--java.lang.ClassNotFoundException | | | +--java.io.IOException | | | | | +--java.io.FileNotFoundException | | | +--java.lang.RuntimeException | | | +--java.lang.NullPointerException | | | +--java.lang.IndexOutOfBoundsException | | | +--java.lang.ArrayIndexOutOfBoundsException | +--java.lang.Error | +--java.lang.VirtualMachineError | +--java.lang.OutOfMemoryError
If a method is throwing an exception, it should either be surrounded by a try catch block to catch it or that method should have the throws clause in its signature. Without the throws clause in the signature the Java JVM compiler does not know what to do with the exception. The throws clause tells the compiler that this particular exception would be handled by the calling method.
Below is a very simple example which explains the behavior of Throw, Throws, Try, Catch, Finally block in Java.
package com.crunchify.tutorials; import java.io.FileInputStream; import java.io.FileNotFoundException; /** * @author Crunchify.com */ public class CrunchifyThrowThrows { @SuppressWarnings("unused") public static void main(String args[]) throws Exception { FileInputStream crunchifyStream1 = null; FileInputStream crunchifyStream2 = null; String fileName = "Crunchify.txt"; System.out.println("main: Starting " + CrunchifyThrowThrows.class.getName() + " with file name = " + fileName); // get file input stream 1 try { crunchifyStream1 = crunchifyTest1(fileName); } catch (FileNotFoundException ex) { System.out.println("main: Oops, FileNotFoundException caught"); } catch (Exception ex) { System.out.println("main: Oops, genreal exception caught"); } // get file input stream 2 crunchifyStream2 = crunchifyTest2(fileName); System.out.println("main: " + CrunchifyThrowThrows.class.getName() + " ended"); } public static FileInputStream crunchifyTest1(String fileName) throws FileNotFoundException { FileInputStream crunchifyStream = new FileInputStream(fileName); System.out.println("crunchifyTest1: File input stream created"); return crunchifyStream; } public static FileInputStream crunchifyTest2(String fileName) throws Exception { FileInputStream crunchifyStream = null; try { crunchifyStream = new FileInputStream(fileName); } catch (FileNotFoundException ex) { throw new Exception("crunchifyTest2: Oops, FileNotFoundException caught"); //System.out.println("crunchifyTest2: Oops, FileNotFoundException caught"); } finally { System.out.println("crunchifyTest2: finally block"); } System.out.println("crunchifyTest2: Returning from crunchifyTest2"); return crunchifyStream; } }
Result:
main: Starting com.crunchify.tutorials.CrunchifyThrowThrows with file name = Crunchify.txt main: Oops, FileNotFoundException caught crunchifyTest2: finally block Exception in thread "main" java.lang.Exception: crunchifyTest2: Oops, FileNotFoundException caught at com.crunchify.tutorials.CrunchifyThrowThrows.crunchifyTest2(CrunchifyThrowThrows.java:45) at com.crunchify.tutorials.CrunchifyThrowThrows.main(CrunchifyThrowThrows.java:30)
Now just replace line 45 and 46 with below line to see below result:
//throw new Exception("crunchifyTest2: Oops, FileNotFoundException caught"); System.out.println("crunchifyTest2: Oops, FileNotFoundException caught");
New Result:
main: Starting com.crunchify.tutorials.CrunchifyThrowThrows with file name = Crunchify.txt main: Oops, FileNotFoundException caught crunchifyTest2: Oops, FileNotFoundException caught crunchifyTest2: finally block crunchifyTest2: Returning from crunchifyTest2 main: com.crunchify.tutorials.CrunchifyThrowThrows ended
List of all Java Tutorials you may be interested in.
Bonus Tips on Exceptions:
- Normal program execution is immediately branched when an exception is thrown.
- Checked exceptions must be caught or forwarded. This can be done in a try … catch statement or by defining the exception in the method definition.
- The exception is caught by the first catch block whose associated exception class matches the class or a superclass of the thrown exception.
- If no matching catch block is found in the exception chain, the thread containing the thrown exception is terminated.
- The finally block after a try … catch statement is executed regardless whether an exception is caught or not.
- Returning within a finally block breaks the exception chain to the invoker even for uncaught exceptions.