Answers for "finally keyword in java"

3

final finally finalize in java

Final is used to apply restrictions on class, method, and variable.
The final class can't be inherited, final method can't be overridden,
and final variable value can't be changed. Final is a keyword

On the other hand
Finally is used to place important code, it will be executed
whether an exception is handled or not. Finally is a block

Lastly
Finalize is used to perform clean up processing just before an
object is garbage collected. Finalize is a method.
Posted by: Guest on December-05-2020
1

java program for try catch finally

class JavaException {
 public static void main(String args[]) {
  int d = 0;
  int n = 20;
  try {
   int fraction = n / d;
   System.out.println("This line will not be Executed");
  } catch (ArithmeticException e) {
   System.out.println("In the catch Block due to Exception = " + e);
  }
  System.out.println("End Of Main");
 }
}
Posted by: Guest on September-29-2020
0

Java Finally Block

//The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an Exception.

Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code.

A finally block appears at the end of the catch blocks and has the following syntax 

try {
   // Protected code
} catch (ExceptionType1 e1) {
   // Catch block
} catch (ExceptionType2 e2) {
   // Catch block
} catch (ExceptionType3 e3) {
   // Catch block
}finally {
   // The finally block always executes.
}
Example
public class ExcepTest {

   public static void main(String args[]) {
      int a[] = new int[2]; // Size 2
      try {
         System.out.println("Access element three :" + a[3]); // Accessing 3rd element
      } catch (ArrayIndexOutOfBoundsException e) {
         System.out.println("Exception thrown  :" + e);
      }finally { // Always executed no matter what!
         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
Posted by: Guest on August-31-2021
0

finally keyword

It come after try catch block
A finally block of code always executes,
whether or not an exception has occurred.
Posted by: Guest on January-05-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language