File tree 1 file changed +32
-0
lines changed
1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Author: Atharv Damle
2
+ // To use try catch to handle exceptions (Runtime errors).
3
+ // The code in catch block is executed if any error occurs in the try block.
4
+ // Full Question: https://www.hackerrank.com/challenges/java-exception-handling-try-catch/problem
5
+
6
+ import java .io .*;
7
+ import java .util .*;
8
+
9
+ public class TryCatch {
10
+
11
+ public static void main (String [] args ) {
12
+ try
13
+ {
14
+ Scanner sc = new Scanner (System .in );
15
+ int a = sc .nextInt ();
16
+ int b = sc .nextInt ();
17
+ System .out .println (a /b );
18
+ sc .close ();
19
+ }
20
+ catch (ArithmeticException e )
21
+ {
22
+ System .out .println (e );
23
+ }
24
+ // Catch any other exception
25
+ catch (Exception e )
26
+ {
27
+ // Here's my code. by using getClass().getName() you handle all other exceptions that may occur
28
+ System .out .println (e .getClass ().getName ());
29
+ }
30
+ }
31
+ }
32
+
You can’t perform that action at this time.
0 commit comments