We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Java
  3. Exception Handling
  4. Java Exception Handling (Try-catch)
  5. Discussions

Java Exception Handling (Try-catch)

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 266 Discussions, By:

recency

Please Login in order to post a comment

  • raavishiva3
    4 days ago+ 0 comments
    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. */
            
            try{
                Scanner sc = new Scanner(System.in);
                int x  = sc.nextInt();
                int y = sc.nextInt();
                int result = x/y;
                System.out.println(result);
            }
            catch(ArithmeticException ae){
                System.out.println("java.lang.ArithmeticException: / by zero");
            }
            catch(InputMismatchException ime){
                System.out.println("java.util.InputMismatchException");
            }
        }
    }
    
    0|
    Permalink
  • aasgha2
    1 week ago+ 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
    
            Scanner scanner = new Scanner(System.in);
            try {
            int x = scanner.nextInt();
            int y = scanner.nextInt();
             System.out.println(x/y);
            } catch (InputMismatchException e) {
                System.out.println(e.getClass().getName());
            } catch (Exception e) {
                System.out.println(e);
            } finally {
                scanner.close();
            }
        }
    }
    
    0|
    Permalink
  • jhonjairomoreno1
    2 weeks ago+ 0 comments
    import java.io.*;
    import java.util.*;
    
    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 input = new Scanner(System.in);
            int x;
            int y;
            
            try{
                x = input.nextInt();
                y = input.nextInt();
                float resp = x/y;
                if(resp%10==0){
                    System.out.println(Math.round(resp));
                }else{
                    System.out.println(y);
                }
            
            }catch(InputMismatchException e){
                System.out.println(e.getClass().toString().substring(6));
            }catch(ArithmeticException a){
                System.out.println(a);
            }
        }
    }
    
    0|
    Permalink
  • KRITIKSAURAV33
    2 weeks ago+ 0 comments

    Here are the solution of Java Exception Handling (Try-catch) Hacker Rank Solution

    0|
    Permalink
  • michjcrow
    2 weeks ago+ 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            String x1 = scan.nextLine();
            String y1 = scan.nextLine();
            try{
                int x = Integer.parseInt(x1);
                int y = Integer.parseInt(y1);
                if(x==0||y==0){
                    System.out.println("java.lang.ArithmeticException: / by zero");
                }
                else{
                    System.out.println(x/y);
                }
            }
            catch(Exception ex){
                System.out.println("java.util.InputMismatchException");
            }
        }
    }
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy