can we keep other statements in between try catch and finally blocks?

No, we cannot keep other statements in between try catch and finally blocks.

public class Main { 
 
	public static void main(String[] args) 
	{ 
		int num1 = 10, num2 = 0; 
 
		// Try to divide by zero 
		try { 
			int result = num1 / num2; 
		} 
		System.out.println("Statement between try and catch.");
		catch (ArithmeticException e) { 
			e.printStackTrace(); 
		} 
		System.out.println("Statement between try and finally.");
		finally {
 
		}
	} 
}

Output

Main.java:8: error: 'try' without 'catch', 'finally' or resource declarations
		try { 
		^
Main.java:12: error: 'catch' without 'try'
		catch (ArithmeticException e) { 
		^
Main.java:16: error: 'finally' without 'try'
		finally {
		^
3 errors

Java interview questions on Exception Handling

Content Protection by DMCA.com
Please Share