Question 31
MediumWhy should catch blocks be ordered from the most specific exception to the most general?
- 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 should catch blocks be ordered from the most specific exception to the most general?
Because Java requires alphabetical order
To make the program faster
It doesn't matter
Otherwise the general handler catches everything first and the specific ones never run
Sign up free to see the answer and a step-by-step explanation.
What is the difference between an Error and an Exception in Java?
Exceptions can't be caught
Errors are serious problems (such as OutOfMemoryError) that applications usually shouldn't catch
They are the same
Errors are always checked
Sign up free to see the answer and a step-by-step explanation.
What does try-with-resources do in Java?
Allocates more memory
Automatically closes resources such as files when the try block ends
Retries the block on failure
Catches every exception silently
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def f():
try:
return 1
finally:
print("finally", end=" ")
return 2
print(f())
finally 1
2
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def f():
try:
return "try"
finally:
print("finally", end=" ")
print(f())
try finally
finally
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def g(x):
try:
r = 10 // x
except ZeroDivisionError:
return -1
else:
return r
finally:
print("done", end=" ")
print(g(0), g(5))
-1 done 2 done
-1 2
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class AppError(Exception): pass
class DbError(AppError): pass
try:
raise DbError("x")
except AppError as e:
print(type(e).__name__)
x
AppError
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
r = []
for v in ["4", "a", "0"]:
try:
r.append(8 // int(v))
except ValueError:
r.append("V")
except ZeroDivisionError:
r.append("Z")
print(r)
[2, 'Z', 'V']
[2, 'V', 'Z']
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
using namespace std;
#include <stdexcept>
int main() {
try {
try {
throw runtime_error("inner");
} catch (const runtime_error &e) {
cout << "1";
throw;
}
} catch (const exception &e) {
cout << "2" << e.what();
}
}
1
12inner
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
using namespace std;
int safeDiv(int a, int b) {
if (b == 0) throw 0;
return a / b;
}
int main() {
int s = 0;
int d[] = {2, 0, 5};
for (int x : d) {
try { s += safeDiv(10, x); }
catch (int) { s += 100; }
}
cout << s;
}
7
107
Sign up free to see the answer and a step-by-step explanation.
finally 2
1
try
finally try
done done -1 2
done -1 done 2
DbError
Exception
[2]
['V', 'V', 'Z']
2inner
1inner
100
5