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.
Loading...
  • Practice
  • Compete
  • Jobs
  • Leaderboard
  • Hiring developers?
  1. Practice
  2. Java
  3. Exception Handling
  4. Java Exception Handling (Try-catch)
  5. Discussions

Java Exception Handling (Try-catch)

  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial

    You are viewing a single comment's thread. Return to all comments →

  • Schuetzl 4 years ago+ 11 comments

    Suggestion: Make the fourth testcase less strict. I had my catch outting...
    java.util.InputMismatchException: For input string: "2147483648"
    When it expected...
    java.util.InputMismatchException
    I simply made another catch for that specific exception and output "java.util.InputMismatchException" directy to work around it but I feel like that shouldn't be necessary. Unless simply printing out the Exception directly is the wrong way to go about it?

    52|
    Permalink
    • abdulhannanali 4 years ago+ 4 comments

      Same here bro same here

      // These were my catches
      catch (InputMismatchException e){
      
      	System.out.println("java.util.InputMismatchException");
      
      }
      catch (Exception e){
      	System.out.println(e);
      }
      
      11|
      ParentPermalink
      • Tuxdude 4 years ago+ 0 comments

        If you use the Scanner class, you only need to catch the InputMismatchException and ArithmeticException to pass all the test cases. There is no need for a catch-all Exception.


        Using a catch-all Exception clause is generally a bad idea when writing programs.

        10|
        ParentPermalink
      • Farahmand 4 years ago+ 4 comments

        Instead of

        System.out.println("java.util.InputMismatchException");
        

        we can use:

        System.out.println(e.getClass().getName());
        
        35|
        ParentPermalink
        • taffarelbergamin 3 years ago+ 0 comments

          Fine, but what's the point of showing an exception message in a different way than the other? If you use that, it will give you an error when in the division by zero test case.

          Why one exception should show the extra content and the other not? This is not even part of the exercise.

          What they could do if they really want a nice representation of the exceptions, could be we print something like "This is an input mismatch exception" and "This is an arithmetic exception".

          This would force people to know which exceptions could happen and don't use the generic Exception.

          7|
          ParentPermalink
        • kencluff 3 years ago+ 3 comments

          I did that, but then testcase 2 failed because it wanted "java.lang.ArithmeticException: / by zero" and you just get "java.lang.ArithmeticException" with this trick.

          1|
          ParentPermalink
          • sergeief 3 years ago+ 0 comments

            you can get "/ by zero" from ecxeption details message

            -4|
            ParentPermalink
          • amarvadla27 2 years ago+ 3 comments
            try{
                        Scanner sc = new Scanner(System.in);
                        int x = sc.nextInt();
                        int y = sc.nextInt();
                        if(y==0)
                            throw  new ArithmeticException("/ by zero");
                        else
                            System.out.println(x/y);
                    }
                    catch(InputMismatchException e){
                        System.out.println(e.getClass().getName());
                    }
                    
                    catch(ArithmeticException e){
                        System.out.println(e);
                    }
            

            `

            0|
            ParentPermalink
            • bmk9727 2 years ago+ 1 comment

              can please explain this System.out.println(e.getClass().getName());

              -1|
              ParentPermalink
              • quirkudoe213 4 weeks ago+ 1 comment

                you prolly already understand this

                0|
                ParentPermalink
                • syndicate27 4 weeks ago+ 0 comments

                  Thank you ^_^

                  0|
                  ParentPermalink
            • syndicate27 2 years ago+ 0 comments

              I look this it looks a lot cleaner then writing out the Arithmetic exception in the catch block. Thank you

              0|
              ParentPermalink
            • allforcatmohit 2 years ago+ 0 comments

              This also works:

              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();
                      try{
                          int result = x/y;
                          System.out.println(result);
                      } catch(Exception e){
                          System.out.println("java.lang.ArithmeticException: / by zero");
                      }
                  } catch(Exception e){
                      System.out.println("java.util.InputMismatchException");
                  }
              }
              

              }

              6|
              ParentPermalink
          • SamratV 2 years ago+ 0 comments

            May be because you are using older version of java. In java 8 this code passes all the testcases successfully.

            try
                    {
                        int a=obj.nextInt();
                        int b=obj.nextInt();
                        System.out.print(a/b);
                    }
                    catch(InputMismatchException e)
                    {
                        System.out.print(e.getClass().getName());
                    }
                    catch(Exception e)
                    {
                        System.out.print(e);
                    }
            
            0|
            ParentPermalink
        • ItzMv 3 years ago+ 1 comment

          Hey can you tell me why the Test case 4th not showing:-

          java.util.InputMismatchException: For input string: "2147483648"
          

          After using e.getClass().getName()

          -1|
          ParentPermalink
          • allforcatmohit 2 years ago+ 0 comments

            Here when you use e.getClass().getName(), .getClass() method will be returning the class of the exception and as we have used getName() method. we will get the name of the exception class.

            so here :java.util.InputMismatchException: For input string: "2147483648"

            class name is just "java.util.InputMismatchException"

            0|
            ParentPermalink
        • aakash_saini_22 2 years ago+ 0 comments

          What is the use of e.getClass().getName()

          0|
          ParentPermalink
      • JimB6800 3 years ago+ 0 comments

        Yes, it's a poor problem.

        I ended up doing...

        } catch (InputMismatchException e) {
          System.out.println(new InputMismatchException());
        }
        

        as a way of stripping off the message.

        2|
        ParentPermalink
      • rlniranjan 9 months ago+ 0 comments

        rror: 'catch' without 'try' catch (InputMismatchException e){ ^

        0|
        ParentPermalink
    • ShafaetChallenge Author 4 years ago+ 3 comments

      It was intentional.

      -31|
      ParentPermalink
      • Schuetzl 4 years ago+ 0 comments

        I had assumed it was. It just seemed to me like an unnecessary hoop to have one jump through is all.

        8|
        ParentPermalink
      • samplenhold 3 years ago+ 0 comments

        If this was in school, I would have told the teacher to scew off. This is garabage. I could have had this challenge done in three lines of code. And having detailed exceptions for one thing and not another thing is a jerky thing to do. It goes against all coding conventions I was taught in school.

        0|
        ParentPermalink
      • b0kater 2 years ago+ 0 comments

        And the purpose is?

        1|
        ParentPermalink
    • SSPIDER 4 years ago+ 0 comments

      thanx dude its work

      0|
      ParentPermalink
    • giaym 3 years ago+ 0 comments

      wait you actually had to have try catches? I was just throwing exceptions around :P, no fun if you just print the stuff without crashing the program

      1|
      ParentPermalink
    • manisha_9801 3 years ago+ 0 comments

      Yes I have also done the same thing

      0|
      ParentPermalink
    • amannepid 3 years ago+ 0 comments

      Another way without any concern of the exception class:

      catch(Exception e){
                  System.out.println(e.getClass().getName() + (e.getMessage() != null && e instanceof ArithmeticException  ? (": " + e.getMessage()) : ""));
              }
      
      0|
      ParentPermalink
    • vinaynit11 3 years ago+ 1 comment

      can't it be possible that , we print the required output by using the object of exception we passed in the catch block .

      Ex :

      try {

      }catch(InputMismatchException i) { System.out.println(i); }

      above program print Exception with the causal , but the required output is without causal ?

      1|
      ParentPermalink
      • manisha_9801 3 years ago+ 1 comment

        By this u handled InputMismatchException......but what about other types of exception How u will handle them

        0|
        ParentPermalink
        • vinaynit11 3 years ago+ 0 comments

          for other exceptions , we will have to first put specific catch block for that specific exceptions still if there is some possibility that other exception might get raised put a generic catch block for handeling all exceptions .

          0|
          ParentPermalink
    • Anu_cool_007 2 years ago+ 0 comments

      Still not fixed :(

      0|
      ParentPermalink
    • AD9000 11 months ago+ 1 comment

      Here's my code. by using getClass().getName() you handle all exceptions that may occur.

      try

      {

              Scanner sc = new Scanner(System.in);
      
              int a = sc.nextInt();
              int b = sc.nextInt();
              System.out.println(a/b);
                          sc.close();
          }
          catch (ArithmeticException e)
          {
              System.out.println(e);
          }
          catch (Exception e)
          {
              System.out.println(e.getClass().getName());
          }
      
      2|
      ParentPermalink
      • aditibajpai2697 8 months ago+ 0 comments

        Great approach, because while taking input only, exception will occur.

        0|
        ParentPermalink
    • vadaszpeter3 1 month ago+ 0 comments
      [deleted]
      0|
      ParentPermalink
    • vadaszpeter3 1 month ago+ 0 comments
      [deleted]
      0|
      ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature