throw and throws in Java: Difference and Examples - Quiz
Total: 5 questions
1. What is the difference between throw and throws in Java?
What is the difference between throw and throws in Java?
throw is an executable statement in a method body: it throws one specific exception object and interrupts execution, for example throw new IOException("File not found"). throws is a part of a method or constructor declaration, written after the parameter list: it throws nothing by itself, but lists, separated by commas, the exception types the method may pass to its caller, for example void read() throws IOException, ParseException. They usually work as a pair: throw inside the method, throws in its header.
2. When is throws mandatory in a method signature, and when can you use throw without throws?
When is throws mandatory in a method signature, and when can you use throw without throws?
throws is mandatory when a method can throw a checked exception and does not catch it itself. Otherwise the compiler reports error: unreported exception IllegalAccessException; must be caught or declared to be thrown. You can do without throws when the thrown exception is a RuntimeException, an Error or a subclass of them, or when the checked exception is caught by a catch block in the same method. The calling method, in turn, must either catch the declared checked exception or declare it in its own throws. Note that throws does not handle the exception; it only shifts the responsibility to the caller.
3. How do you rethrow a caught exception in Java, and what is precise rethrow in Java 7+?
How do you rethrow a caught exception in Java, and what is precise rethrow in Java 7+?
Inside a catch block you write throw e; — the same object is thrown, with its original message and stack trace. This is done when a method must react to the error (write a log entry) but leave the handling to the caller. To change the type, create a new exception and pass the original one as the cause: throw new IllegalStateException("Could not load the order", e). Since Java 7 precise rethrow works: after catch (Exception e) { throw e; } the method may declare in throws only the checked exceptions the try block can really throw (for example, throws IOException instead of throws Exception). The condition is that the parameter e is not reassigned in the catch block.
4. How do you declare multiple exceptions in throws, and how must the calling code handle them?
How do you declare multiple exceptions in throws, and how must the calling code handle them?
List the types separated by commas, in any order: static void load(int mode) throws IOException, ParseException. The calling code must handle every checked exception from the list — with separate catch blocks, with one multi-catch catch (IOException | ParseException e) or with a common superclass; if even one is missed, the code does not compile. throws Exception covers all checked exceptions at once, but hides what can actually go wrong, so it is better to list specific types. A subclass together with its superclass (throws FileNotFoundException, IOException) is allowed, although redundant.
5. What can you put after throw, and what happens with throw null?
What can you put after throw, and what happens with throw null?
Only an expression of type Throwable or its subclass is allowed after throw: a new object (throw new IllegalArgumentException("...")) or the parameter of a catch block. throw "Error"; and throw 42; do not compile: incompatible types: String cannot be converted to Throwable. throw null;, or a reference of a suitable type that equals null, compiles, but at run time a NullPointerException is thrown instead of the intended exception. Besides, throw always completes the current block: a statement right after it causes the unreachable statement error, and a method that returns a value may end with throw and no return.