What is the Try Block in Java



A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following –

Syntax

try {
   // Protected code
} catch (ExceptionName e1) {
   // Catch block
}

The code which is prone to exceptions is placed in the try block. When an exception raised inside a try block, instead of terminating the program JVM stores the exception details in the exception stack and proceeds to the catch block.

Updated on: 2020-02-25T10:44:22+05:30

505 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements