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

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • 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 280 Discussions, By:

recency

Please Login in order to post a comment

  • waghkrutika01
    2 days ago+ 0 comments

    done under Java 8

    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 sc =  new Scanner(System.in);
            
            try{
                int x = sc.nextInt();
                int y = sc.nextInt();
                int res = x / y;
                System.out.println(res);
            }
            catch(InputMismatchException ime) {
                System.out.println("java.util.InputMismatchException");
            }
            catch(ArithmeticException ae) {
                System.out.println("java.lang.ArithmeticException: / by zero");
            }
            finally{
                sc.close();
            }
        }
    }
    
    0|
    Permalink
  • md_shamim_ahamed
    3 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. */
            Scanner sc=new Scanner(System.in);
            try{
            int a=sc.nextInt();
            int b=sc.nextInt();
            int c=a/b;
            System.out.println(c);
            }catch(InputMismatchException ie){
                System.out.println("java.util.InputMismatchException");
            }catch(ArithmeticException ae){
                System.out.println("java.lang.ArithmeticException: / by zero");
            }
            
        }
    }
    
    0|
    Permalink
  • akashpande1005
    1 week ago+ 0 comments

    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 a = sc.nextInt();
            int b  = sc.nextInt();
            System.out.println(a/b);
        }
        catch(InputMismatchException ime){
            System.out.println("java.util.InputMismatchException");
        }
        catch(ArithmeticException ae){
            System.out.println("java.lang.ArithmeticException: / by zero");
        }
        finally{
            sc.close();
        }jgj
    }
    

    }

    0|
    Permalink
  • bisenpriyanshus1
    1 week ago+ 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
            Scanner scan= new Scanner (System.in);
            try {
                   int a=scan.nextInt();
        int b=scan.nextInt();
        int c=a/b;
        System.out.println(c);
        }catch(InputMismatchException ie){
            System.out.println("java.util.InputMismatchException");
        }catch(ArithmeticException ae){
            System.out.println("java.lang.ArithmeticException: / by zero");
        }
    
    }
    

    }

    0|
    Permalink
  • shameenbasha066
    2 weeks 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. */
            Scanner sc=new Scanner(System.in);
            try{
            int a=sc.nextInt();
            int b=sc.nextInt();
            int c=a/b;
            System.out.println(c);
            }catch(InputMismatchException ie){
                System.out.println("java.util.InputMismatchException");
            }catch(ArithmeticException ae){
                System.out.println("java.lang.ArithmeticException: / by zero");
            }
            
        }
    }
    
    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