Question 71
HardWhy is catching Throwable or Error in Java usually a mistake?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
Why is catching Throwable or Error in Java usually a mistake?
Errors like OutOfMemoryError signal serious problems the program can't normally recover from
It is required
It catches nothing
Throwable can't be caught
Sign up free to see the answer and a step-by-step explanation.
Which exception type should a method throw when called with an argument that breaks its contract, such as a negative size?
ClassNotFoundException
IOException
IllegalArgumentException
OutOfMemoryError
Sign up free to see the answer and a step-by-step explanation.
What is the cost of exceptions in C++ when none is thrown, with modern 'zero-cost' implementations?
Every function call doubles in time
Each try block allocates memory
Almost none at run time; the cost is mainly larger binaries and slow throws
Exceptions are always slow
Sign up free to see the answer and a step-by-step explanation.
What does Python's 'except Exception:' NOT catch?
ValueError
ZeroDivisionError
KeyError
KeyboardInterrupt and SystemExit, which derive from BaseException
Sign up free to see the answer and a step-by-step explanation.
When should a function return an error value (or Optional) instead of throwing?
When the 'failure' is an expected, common outcome, such as a lookup that may find nothing
Only in constructors
Never
Always
Sign up free to see the answer and a step-by-step explanation.
Why is 'catch and ignore' (an empty catch block) harmful?
It doesn't compile
It is slow
Errors disappear silently, leaving the program in a bad state with no trace
It causes recursion
Sign up free to see the answer and a step-by-step explanation.
What is exception translation (wrapping)?
Catching a low-level exception and throwing a higher-level one that makes sense to callers, keeping the cause
Converting exceptions to integers
Ignoring exceptions
Translating messages into other languages
Sign up free to see the answer and a step-by-step explanation.
What does the 'finally' block guarantee in Java?
It runs before the try block
It runs whether the try block completes normally, returns or throws (unless the JVM stops)
It runs only on errors
It runs only on success
Sign up free to see the answer and a step-by-step explanation.
Which Java exception is checked?
NullPointerException
IOException
IllegalStateException
ArithmeticException
Sign up free to see the answer and a step-by-step explanation.
What does std::terminate do?
Restarts the function
Ignores the exception
Ends the program, by default calling abort()
Throws a new exception
Sign up free to see the answer and a step-by-step explanation.