In this Java tutorial we want to learn about Try Catch Blocks in Java, Java is popular programming language, Java is used for developing different types of applications. if you are a programmer than you will know that in any programming language errors and exceptions can occur, so the same will be in Java programming language. for handling such situations, Java provides a way called try catch blocks. in this article we want to learn what try catch blocks are and how they can be used in Java programming.
What are Java Try-Catch Blocks ?
So try catch block is a structure used to catch and handle exceptions in Java. try block contains the code that might throw an exception, and catch block contains the code that handles the exception. in programming if an exception is thrown in the try block, catch block is executed, and the program can recover from the exception.
So now let’s talk about the syntax of try catch block, this is the syntax.
1 2 3 4 5 |
try { // code that might throw an exception } catch (ExceptionType e) { // code to handle the exception } |
So in the above code try block contains the code that might throw an exception. if an exception is thrown, Java will look for a catch block that can handle exception. catch block specifies the type of exception that it can handle. if the exception type matches the type of the exception thrown in the try block, the catch block is executed.
Handling Multiple Exceptions in Java
Using Java, you can handle multiple exceptions in single try catch block. this can be useful when you want to handle different types of exceptions in different ways. this is the syntax for handling multiple exceptions:
1 2 3 4 5 6 7 8 9 |
try { // code that might throw an exception } catch (ExceptionType1 e1) { // code to handle the first type of exception } catch (ExceptionType2 e2) { // code to handle the second type of exception } catch (ExceptionType3 e3) { // code to handle the third type of exception } |
Java also provides finally block that can be used to execute code regardless of whether an exception is thrown or not. this is the syntax for try catch finally block:
1 2 3 4 5 6 7 |
try { // code that might throw an exception } catch (ExceptionType e) { // code to handle the exception } finally { // code that will always be executed } |
Now let’s create a practical example, for example we have a program that reads data from a file and processes it. but if file is not found or cannot be read, our program should handle the exception and notify the user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class FileProcessor { public static void main(String[] args) { try { File file = new File("data.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); // process the line } scanner.close(); } catch (FileNotFoundException e) { System.out.println("File not found: " + e.getMessage()); } } } |
Subscribe and Get Free Video Courses & Articles in your Email