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 finally Keyword

try {
  int[] myNumbers = {1, 2, 3};
  System.out.println(myNumbers[10]);
} catch (Exception e) {
  System.out.println("Something went wrong.");
} finally {
  System.out.println("The 'try catch' is finished.");
}
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 :: lcm of two number in java 
Java :: how to remove last latter in word in java 
Java :: java switch statement 
Java :: java final modifier on method 
Java :: broadcast receiver for no interner android studio 
Java :: room insert and return id 
Java :: what is ioexception in java 
Java :: Adding Entire ArrayList to another ArrayList in Java 
Java :: meaning of instantiated in java 
Java :: generate hash in java 
Java :: android how to know when snackbar is done 
Java :: format specifier in java 
Java :: java comment 
Java :: java add values to array 
Java :: Exception handling using try...catch 
Java :: radix sort 
Java :: Java short Keyword 
Java :: java convert int to string 
Java :: minecraft addlayer(u) in the type rendererlivingentity<t is not applicable for the arguments 
Java :: java evaluate two expressions in if statemenmt 
Java :: intellij error for new project 
Java :: teimpo en segundos java 
Java :: Execute method on load alternative 
Java :: android system navigation back bar hide 
Java :: Demo Example 
Java :: longest subarray with equal 0 and 1 
Java :: java object class 
Java :: another name for coffee 
Java :: set integer array value to null java 
Java :: banner generator spring boot 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =