Skip to content

Commit 15eae36

Browse files
authored
Update java_exception_handling(try-catch).java
1 parent 4e568fb commit 15eae36

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

Hackerrank/java_exception_handling(try-catch).java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*Exception handling is the process of responding to the occurrence, during computation, of exceptions – anomalous or exceptional conditions requiring special processing – often changing the normal flow of program execution. (Wikipedia)
2+
3+
Java has built-in mechanism to handle exceptions. Using the try statement we can test a block of code for errors. The catch block contains the code that says what to do if exception occurs.
4+
5+
This problem will test your knowledge on try-catch block.
6+
7+
You will be given two integers and as input, you have to compute . If and are not bit signed integers or if is zero, exception will occur and you have to report it. Read sample Input/Output to know what to report in case of exceptions.
8+
*/
9+
110
import java.io.*;
211
import java.util.*;
312
import java.text.*;
@@ -11,15 +20,19 @@ public static void main(String[] args) {
1120

1221
Scanner scan = new Scanner(System.in);
1322
try {
14-
int x = scan.nextInt();
23+
//taking 2 numbers inputs
24+
int x = scan.nextInt();
1525
int y = scan.nextInt();
26+
//dividing 2 numbers
1627
System.out.println(x / y);
1728
}
1829
catch(ArithmeticException | InputMismatchException e) {
1930
if (e instanceof ArithmeticException) {
31+
//if there is arithmetic exception occured in try block then it will be catched here
2032
System.out.println("java.lang.ArithmeticException: / by zero");
2133
} else if (e instanceof InputMismatchException){
2234
System.out.println("java.util.InputMismatchException");
35+
//if there is inputmismatch exception occured in try block then it will be catched here
2336
}
2437
}
2538
scan.close();

0 commit comments

Comments
 (0)