Java - Exception HandlingWhich of the following method signatures correctly declares a checked exception?Apublic void readFile() throws NullPointerExceptionBpublic void readFile() throws IOExceptionCpublic void readFile() throws ErrorDpublic void readFile() throws RuntimeExceptionCheck Answer
Step-by-Step SolutionSolution:Step 1: Identify checked exceptions in optionsIOException is a checked exception and must be declared.Step 2: Check other optionsNullPointerException, Error, and RuntimeException are unchecked or errors, so no need to declare.Final Answer:public void readFile() throws IOException -> Option BQuick Check:Checked exceptions require throws declaration [OK]Quick Trick: Only checked exceptions need throws declaration [OK]Common Mistakes:Declaring unchecked exceptions with throwsConfusing Error with ExceptionMissing throws for checked exceptions
Master "Exception Handling" in Java9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Java Quizzes Abstraction - Abstract methods - Quiz 10hard Abstraction - Abstract vs concrete classes - Quiz 8hard Classes and Objects - Class definition - Quiz 8hard Constructors - Constructor overloading - Quiz 5medium Constructors - This keyword usage - Quiz 4medium Custom Exceptions - Why custom exceptions are needed - Quiz 5medium Exception Handling - Try–catch block - Quiz 6medium Exception Handling - Try–catch block - Quiz 12easy Inheritance - Inheritance limitations - Quiz 8hard Interfaces - Interface declaration - Quiz 9hard