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 :: How do you make bedrock full screen in Minecraft? 
Java :: update a key in the firebase realtime database java 
Java :: customized cache key java 
Java :: BufferedReader x = new BufferedReader(new InputStreamReader(System.in)); 
Java :: java stream dristinct 
Java :: colors java intell print 
Java :: how to change default port in spring boot 
Java :: assign a random number in a set without replacement javva 
Java :: Java program to print the character or a letter x using star 
Java :: jbutton actionperformed 
Java :: rotate vector 3d java 
Java :: how to repeat string in java 
Java :: REGEX ___ get length of array in java 
Java :: android java string animations 
Java :: XmlRootElement Object to String 
Java :: longest subarray with equal 0 and 1 
Java :: spigot disable health regeneration 
Java :: filterreader converts a string to uppercase java 
Java :: how to install openjdk 16 
Java :: data validation dialog box android 
Java :: javafx open alert window 
Java :: unlock the screen 
Java :: how to mutate value in vector in java 
Java :: extended window style values in java 
Java :: how to import a self written class in java 
Java :: mint menu disapeared 
Java :: how to print message to console java 
Java :: javafx line chaart duplicate series added 
Java :: spring-boot java header Content-Type constant 
Java :: Java 8 merge multiple collections. 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =