Java Exception Handling (Try-catch) Hackerrank Solution
For Explanation Watch Video:
Sample Input 0:
10
3
Sample Output 0:
3
Sample Input 1:
10
Hello
Sample Output 1:
java.util.InputMismatchException
Sample Input 2:
10
0
Sample Output 2:
java.lang.ArithmeticException: / by zero
Sample Input 3:
23.323
0
Sample Output 3:
java.util.InputMismatchException
Code:
import java.io.*;import java.util.*;import java.text.*;import java.math.*;import java.util.regex.*;
public class Solution {
public static void main(String[] args) { try{ Scanner scn = new Scanner(System.in); int x = scn.nextInt(); int y = scn.nextInt(); System.out.println(x/y); }catch(InputMismatchException e){ System.out.println(e.getClass().getName()); }catch(ArithmeticException e){ System.out.println(e); } }}
Comments
Post a Comment