If we write System.exit(0) inside try block then finally block will be execute or not?

If we write System.exit(0) inside try block then finally block will be execute. method system.exit(0) does throw a exception, catch and finally statements are also executed.

System. exit(0) method terminates the current running program. If the status is 0, it indicates the termination is successful.

Here 10 can not divided by zero that’s why its throw Arithmetic exception and pick by catch block.

Finally block always execute no matter exception is occurred or not.

				
					public class SystemException {
	public static void main(String[] args) {
		try {
			int i = 10 / 0;
			System.exit(0);
		} catch (ArithmeticException ex) {
			ex.printStackTrace();
		} finally {
			System.out.println("good");
		}
	}
}
				
			

Output :-
java.lang.ArithmeticException: / by zero
at SystemException.main(SystemException.java:4)
good

If we write System.exit(0) inside try block then finally block will be execute or not? Read More »