- 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
Can finally block be used without catch in Java?
Yes, it is not mandatory to use catch block with finally. You can have to try and finally.
Example
public class Test { public static void main(String args[]) { int a[] = new int[2]; try { } finally { a[0] = 6; System.out.println("First element value: " + a[0]); System.out.println("The finally statement is executed"); } } }
Output
First element value: 6 Exception in thread "main" The finally statement is executed java.lang.ArrayIndexOutOfBoundsException: 3 at a5Exception_Handling.Test.main(Test.java:7)
- Related Articles
- Can we have a try block without a catch block in Java?\n
- Explain Try/Catch/Finally block in PowerShell
- Why variables defined in try cannot be used in catch or finally in java?
- Can we declare a try catch block within another try catch block in Java?
- Can we have an empty catch block in Java?
- Can we to override a catch block in java?
- Can a try block have multiple catch blocks in Java?
- What is the finally block in Java?
- What are try, catch, finally blocks in Java?
- Does code form Java finally block
- Can we write any statements between try, catch and finally blocks in Java?
- Flow control in a try catch finally in Java
- Flow control in try catch finally in Java programming.
- Can we have a return statement in the catch or, finally blocks in Java?
- Is finally block always get executed in Java?

Advertisements