0
0
Javaprogramming~5 mins

Throws keyword in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ACreates a new exception object
BDeclares exceptions that the method might throw
CCatches exceptions inside the method
DPrevents exceptions from occurring
Which of these is a correct way to declare multiple exceptions using throws?
Avoid example() throws IOException, SQLException
Bvoid example() throws IOException SQLException
Cvoid example() throw IOException, SQLException
Dvoid example() throws (IOException, SQLException)
If a method throws a checked exception but does not declare it with throws, what happens?
AThe exception is ignored at runtime
BThe program compiles and runs fine
CThe compiler gives an error
DThe method automatically catches the exception
Which keyword is used to actually throw an exception object inside a method?
Athrow
Bthrows
Ccatch
Dtry
Is it necessary to declare unchecked exceptions with throws?
AOnly if the method catches them
BNo, never
CYes, always
DOptional, but usually not declared
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.