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 :: what is arraylist 
Java :: android list tostring 
Java :: java string reduce 
Java :: how to use setonclicklistener from custom view in android 
Java :: how to call same activity again in android 
Java :: java stack verification failed 
Java :: android press back 2 times to exit 
Java :: string java 
Java :: take a peice from array java 
Java :: Write code to declare an array that will hold calendar months (.e. January to December) java 
Java :: what is object in java 
Java :: how to find odd and even in a array 
Java :: seek bar 
Java :: log messages not printing in springboot using org.slf4j.Logger; 
Java :: fix nullpointerexception 
Java :: java variable 
Java :: java map key set 
Java :: how to find a word in a statement java 
Java :: FlutterFirebaseCorePlugin.java uses or overrides a deprecated API. 
Java :: java delete element from list 
Java :: declaration in java 
Java :: java how to split a string to array 
Java :: data structure java 
Java :: different constructiors in java and what they do explained 
Java :: java startswith regex 
Java :: android hide and show bottom navigation 
Java :: what is return method 
Java :: java programming problems 
Java :: javafx 
Java :: calculate the area of two squares in java by using a method 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =