HackerRank/JAVA

Java Exception Handling (Try-catch)

예쁜꽃이피었으면 2024. 1. 22. 16:31

https://www.hackerrank.com/challenges/java-exception-handling-try-catch/problem?isFullScreen=true

 

Java Exception Handling (Try-catch) | HackerRank

Use try-catch to test a block of code for errors.

www.hackerrank.com

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) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        try{
            
            int x = sc.nextInt();
            int y = sc.nextInt();
        
            // System.out.println(x%y);
            System.out.println(x/y);
        }catch(InputMismatchException mi){
            System.out.println("java.util.InputMismatchException");
        }catch(ArithmeticException ari){
            System.out.println("java.lang.ArithmeticException: / by zero");
        }
    }
}

 

 


 

 

 

 

 

 

 

 

 

반응형

'HackerRank > JAVA' 카테고리의 다른 글

Java 1D Array  (1) 2024.01.22
Java BigInteger  (0) 2024.01.22
Java Primality Test  (0) 2024.01.22
Valid Username Regular Expression  (0) 2024.01.19
[작성중]Java Regex 2 - Duplicate Words  (0) 2024.01.19