- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Flow control in try catch finally in Java programming.
An exception is an issue (run time error) that occurred during the execution of a program. Some exceptions are prompted at compile time these are known at compile-time exceptions or checked exceptions.
If an exception occurs the program terminates abruptly at the line that caused exception, leaving the remaining part of the program unexecuted. To prevent this, you need to handle exceptions.
Try, catch, finally blocks
To handle exceptions Java provides a try-catch block mechanism.
The try block − A try block is placed around the code that might generate an exception. Code within a try/catch block is referred to as a protected code.
Syntax
try { // Protected code } catch (ExceptionName e1) { // Catch block }
The catch block − A catch block involves declaring the type of exception you are trying to catch. If an exception occurs in the try block, the catch block (or blocks) that follows the try is checked. If the type of exception that occurred is listed in a catch block, the exception is passed to the catch block much as an argument is passed into a method parameter.
The finally block − The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of the occurrence of an Exception.
Flow control in exceptions
In exception handling, you can have try-catch, try-finally and, try-catch-finally blocks. In these, you might have the following scenarios −
If Exception doesn’t occur − If no exception raised in the try block the code in the catch block (in try-catch or try-catch-finally) doesn’t get executed. Anyhow the finally block gets executed always (try-finally or, try-catch-finally)
If Exception occurs in try-catch − 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.
If the type of exception that occurred is listed/handled in a catch block, the exception is passed to the catch block much as an argument is passed into a method parameter and, the code in the (respective) catch block is executed.
Example
import java.io.File; import java.io.FileInputStream; public class Test { public static void main(String args[]){ System.out.println("Hello"); try{ File file =new File("my_file"); FileInputStream fis = new FileInputStream(file); } catch(Exception e){ System.out.println("Given file path is not found"); } } }
Output
Given file path is not found
If the occurred exception is not handled in the catch block and thrown using the throws keyword. The code in the final block gets executed and the exceptions occur at the run time.
Example
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Test { public static void main(String args[]) throws FileNotFoundException{ System.out.println("Hello"); try{ File file =new File("my_file"); FileInputStream fis = new FileInputStream(file); } catch(ArrayIndexOutOfBoundsException e){ } finally{ System.out.println("finally block"); } } }
Output
Runtime Exception
Hello finally block Exception in thread "main" java.io.FileNotFoundException: my_file (The system cannot find the file specified) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(Unknown Source) at java.io.FileInputStream.<init>(Unknown Source) at sample.Test.main(Test.java:12)
Exception occurs in try-catch-finally
The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of the occurrence of an Exception.
Therefore, if an exception occurs in a try-catch-finally block. The code in the catch block, as well as finally block, gets executed.
Example
public class ExcepTest { public static void main(String args[]) { int a[] = new int[2]; try { System.out.println("Access element three :" + a[3]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Exception thrown :" + e); } finally { a[0] = 6; System.out.println("First element value: " + a[0]); System.out.println("The finally statement is executed"); } } }
Output
Exception thrown : java.lang.ArrayIndexOutOfBoundsException: 3 First element value: 6 The finally statement is executed
- Related Articles
- Flow control in a try catch finally in Java
- Flow control in try catch finally in C#
- Try-Catch-Finally in C#
- What are try, catch, finally blocks in Java?
- Try/catch/finally/throw keywords in C#
- Explain Try/Catch/Finally block in PowerShell
- Why variables defined in try cannot be used in catch or finally in java?
- Can we write any statements between try, catch and finally blocks in Java?
- How do we use try...catch...finally statement in JavaScript?
- Can we declare a try catch block within another try catch block in Java?
- Try, catch, throw and throws in Java
- Static Control Flow in Java
- Can finally block be used without catch in Java?
- The try-finally Clause in Python
- Can a try block have multiple catch blocks in Java?
