What is Java try block?
The Java try block is used to enclose statements where an exception can occur. It ensures that if an unexpected exception occurs it should not break the flow of the program. If an exception occurs at the particular statement inside the try block, the rest of the statement will not execute and control will jump to catch block. The try block must be followed by a catch block, finally block, or both.
What is Java catch block?
The catch block is used to handle the exception. The catch block will only execute if an error occurs in the try block. A single try block can have multiple catch blocks associated with it you can have different catch block for a different exception.
Syntax
The syntax for try & catch is as follows –
try { // Body of try block. // Statements that may cause an exception } catch (<exception_class> <object_name>) { // Body of catch block. // Handling an exception }
Examples
Following are the examples of try and catch that might help you understand better –
Arithmetic exception
The arithmetic exception is thrown when an exceptional arithmetic condition has occurred.
public class TryCatchExample1 { public static void main(String[] args) { int numA = 10; int numB = 0; try { // Divide a number by zero. System.out.println(numA / numB); } catch (ArithmeticException ex) { // Handle the exception here System.out.println("You should not divide a number by zero"); } } }
ArrayIndexOutOfBounds exception
The ArrayIndexOutOfBoundsException is thrown to indicate that an array has been accessed with an illegal index.
public class TryCatchExample2 { public static void main(String[] args) { // Array of 4 elements (0 to 3) int array[] = {111, 22, 345, 400}; try { // Let try to access the 5th element System.out.println(array[4]); } catch (ArrayIndexOutOfBoundsException e) { // Handle the exception here System.out.println("Invalid array index!"); } } }
NullPointer exception
The NullPointerException is thrown when an application attempts to use null in a case where an object is required.
public class TryCatchExample3 { public static void main(String[] args) { // A null string String str = null; try { // Let try to get length of null string System.out.println(str.length()); } catch (NullPointerException e) { // Handle the exception here System.out.println("String is null!"); } } }
StringIndexOutOfBounds exception
The StringIndexOutOfBoundsException is Thrown by String methods to indicate that an index is either negative or greater than the size of the string.
public class TryCatchExample4 { public static void main(String[] args) { // A string of 5 characters String str = "hello"; try { // Let try to access 6th character char ch = str.charAt(5); } catch (StringIndexOutOfBoundsException e) { // Handle the exception here System.out.println("Invalid character index!"); } } }
NumberFormat exception
The NumberFormatException is thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.
public class TryCatchExample5 { public static void main(String[] args) { // A string containing a word String str = "hello"; try { // Let try to convert word "hello" to number int num = Integer.parseInt(str); } catch (NumberFormatException e) { // Handle the exception here System.out.println("Invalid number provided!"); } } }
FileNotFound exception
The FileNotFoundException is thrown when a file with the specified pathname does not exist.
import java.io.FileNotFoundException; import java.io.FileReader; public class TryCatchExample6 { public static void main(String[] args) { // File name String fileName = "sample.txt"; try { // Create a instance of file reader FileReader reader = new FileReader(fileName); } catch (FileNotFoundException ex) { // Handle the exception here System.out.println("File [sample.txt] not found!"); } } }