Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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");
 }
}
Comment

Explain try & catch finally block in Java

try block: code that is protected for any exceptions. and it is mandatory (only try)
catch block: if any exception happens during runtime in the try block, 
the catch block will catch that exception.
if any exception happens during runtime in the try block, 
control will be given to catch block.
An optional finally block gives us a chance to run the code which 
we want to execute EVERYTIME a try-catch block is completed 
– either with errors or without any error.
Comment

Java finally block

try {
  //code
}
catch (ExceptionType1 e1) { 
  // catch block
}
finally {
  // finally block always executes
}
Comment

Java try...finally block

class Main {
  public static void main(String[] args) {
    try {
      int divideByZero = 5 / 0;
    }

    finally {
      System.out.println("Finally block is always executed");
    }
  }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java types of list 
Java :: spring boot get request body 
Java :: remove last character from stringbuffer 
Java :: socket in java 
Java :: combine two array in java 
Java :: java base64 to file 
Java :: view all certificates from keystore java 
Java :: the main of java 
Java :: recursion examples java 
Java :: how to compare strings java 
Java :: System.out.printf("%"+(n)+"s ") 
Java :: Calling User-Defined Method in Java 
Java :: not equal java 
Java :: hashmap get value by key java 
Java :: create a hashmap 
Java :: java character 
Java :: smart ways to print a matrix in java 
Java :: destory fragment 
Java :: android java show hide keyboard: 
Java :: long to int java 
Java :: Java Access ArrayList Elements 
Java :: what is exception in java 
Java :: pass array to method java 
Java :: reverse loop in java 
Java :: can i call another function from main hava 
Java :: method parameters in java 
Java :: How to implement the A* shortest path algorithm, in Java? 
Java :: Error: Could not find or load main class Main Caused by: java.lang.ClassNotFoundException: Main 
Java :: android text change java debounce 
Java :: afficher matrice java 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =