A Little Fun With Try-Catch
We'll walk through multiple cases using the try-catch block in a quiz-based style.
Let’s briefly go over the syntax and then jump straight into the cases.
Press + to interact
class Exceptions {public static void main( String args[] ) {try{// exception thrown here}catch(Exception e){// exception caught here}}}
The try
block contains the block of code where you (the programmer) believe an exception might occur e.g., the user enters 0 as an input for a divide operation, which might result in an ArithmeticException
. To prevent the system from breaking, those exceptions are handled in the catch block.
Cases
Here, we’ll discuss different variations in which the try-catch block can occur and you can test your understanding by completing the quizzes below.