Recall & Review
beginner
What is the purpose of the
throws keyword in Java?The
throws keyword is used in a method signature to declare that the method might throw certain checked exceptions. It tells the caller to handle or further declare these exceptions.Click to reveal answer
beginner
How does
throws differ from throw in Java?throw is used to actually throw an exception object inside a method, while throws declares that a method might throw exceptions, informing the caller to handle them.Click to reveal answer
intermediate
Can a method declare multiple exceptions with
throws? How?Yes, a method can declare multiple exceptions by listing them separated by commas after the
throws keyword, for example: void myMethod() throws IOException, SQLException.Click to reveal answer
beginner
What happens if a method does not handle a checked exception and does not declare it with
throws?The Java compiler will produce an error because checked exceptions must be either caught with a try-catch block or declared with
throws in the method signature.Click to reveal answer
intermediate
Is it mandatory to declare unchecked exceptions with
throws?No, unchecked exceptions (like
RuntimeException) do not need to be declared with throws. It is optional because they can occur anywhere and are not checked at compile time.Click to reveal answer
What does the
throws keyword do in a Java method?✗ Incorrect
The
throws keyword declares exceptions that the method might throw, informing callers to handle them.Which of these is a correct way to declare multiple exceptions using
throws?✗ Incorrect
Multiple exceptions are declared separated by commas after the
throws keyword without parentheses.If a method throws a checked exception but does not declare it with
throws, what happens?✗ Incorrect
The compiler requires checked exceptions to be declared or caught; otherwise, it produces an error.
Which keyword is used to actually throw an exception object inside a method?
✗ Incorrect
throw is used to throw an exception object, while throws declares exceptions.Is it necessary to declare unchecked exceptions with
throws?✗ Incorrect
Unchecked exceptions do not have to be declared with
throws, but it is allowed.Explain the role of the
throws keyword in Java methods and how it helps with exception handling.Think about how Java forces you to handle certain errors.
You got /4 concepts.
Describe the difference between
throw and throws keywords in Java with examples.One is for declaring, the other is for actually throwing.
You got /4 concepts.