Skip to content

Commit 9acd361

Browse files
authored
Create TryCatch.java
1 parent 3ba644f commit 9acd361

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Exception Handling/TryCatch.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+

0 commit comments

Comments
 (0)