DEV Community

Deepikandas
Deepikandas

Posted on

#30 Known is a Drop! Tricky questions- Exception Handling Part 4

Q1.
try {

} finally {

}

No compile error.
try should be followed either by catch or finally
Enter fullscreen mode Exit fullscreen mode
Q2.
try {

} catch(Exception e) {

}

No compilation error.
try followed by catch

Enter fullscreen mode Exit fullscreen mode
Q3.
try {

} catch(Exception e) {

} finally {

}

Answer: This is the right order: 
if catch is present ,then it must be followed by try.1.try 2.catch 3.finally
Enter fullscreen mode Exit fullscreen mode
4.try {

} catch(ArithmeticException e) {

} catch(Exception e) {

}

Ans: multiple catch allowed.
catch with Exception should be last of all other exceptions.

Enter fullscreen mode Exit fullscreen mode
Q5.
try {

} catch(Exception e) {

} catch(ArithmeticException e) {

}
Answer: compile error.
Catch(Exception e) should be last .
Enter fullscreen mode Exit fullscreen mode
Q6.
try {

} finally {

} catch(Exception e) {

}
Answer: compile error.
Catch() block cannot be after finally block

Enter fullscreen mode Exit fullscreen mode
Q7.try {

} catch(Exception e) {

} finally {

} finally {

}

Ans: Multiple finally not allowed
Enter fullscreen mode Exit fullscreen mode
Q8.
try {

} catch(ArithmeticException e) {

} finally {

} catch(Exception e) {

}
Answer: cath block is after finally block.compile error
Enter fullscreen mode Exit fullscreen mode
Q9.try {

}
Ans: Compile error.
Either finally or catch block should follow
Enter fullscreen mode Exit fullscreen mode
Q10.
catch(Exception e) {

}
Ans: catch block cannot be alone without try block
Enter fullscreen mode Exit fullscreen mode
Q11.
11.
finally {

}

Ans: finally block cannot alone without try block
Enter fullscreen mode Exit fullscreen mode
Q12.
try {

} catch(ArithmeticException e) {

} catch(ArithmeticException e) {

}
Ans: cannot have two catch blocks for the same exception type.
Enter fullscreen mode Exit fullscreen mode
Q13.
try {

} catch(Exception e) {

} catch(RuntimeException e) {

}

Catch block with Exception should be last .
Enter fullscreen mode Exit fullscreen mode
Q14.
try {

} catch(RuntimeException e) {

} catch(Exception e) {

}
Answer: Valid
Enter fullscreen mode Exit fullscreen mode
15.
try {

} finally {

} finally {

}
Ans: Multiple finally block not allowed
Enter fullscreen mode Exit fullscreen mode

Top comments (0)