Exception thrown in catch and finally clause

Finally clause is executed even when exception is thrown from anywhere in try/catch block. if both catch and finally blocks try to throw Exception, then the Exception in catch is swallowed and only the exception in finally will be thrown in the end.
here main try block throw exception and its pick by catch block.after that inside finally block try statement is execute and its throw exception.now catch block is execute and throw latest exception.

				
					public class FinalyBlockExc {
	public static void main(String[] args) {
		try {
			int i = 10 / 0;
		} catch (ArithmeticException ex) {
			System.out.println("Arithmetic block Exception:- " + ex.getMessage());
		} finally {
			try {
				int ar[] = { 1, 2, 3 };
				System.out.println("try in finally block:- " + ar[100]);
			} catch (Exception ex) {
				System.out.println("Exception in finally block:- " + ex.getMessage());
			}
		}
	}
}
				
			

Output :-
Arithmetic block Exception:- / by zero
Exception in finally block:- 100

Leave a Comment

Your email address will not be published. Required fields are marked *