Try/Catch exception help in Java










1















The program I am making has a console-based text menu to select an option between 1 and 2, I need it to catch for any input that isnt a number, and isnt a number between 1 and 2. This is what I have



 Scanner scan = new Scanner(System.in);
int number = 0;
try
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();

catch (ArithmeticException e)
System.out.println("Arithmetic Exception");

catch (Exception e)
if (!(number == 1)


Any insight on where I have gone wrong is appreciated!










share|improve this question

















  • 2





    You say you’ve gone wrong - where does the actual behaviour differ from the intended?

    – MTCoster
    Nov 14 '18 at 20:00






  • 1





    What's the problem? You didn't specify.

    – NiVeR
    Nov 14 '18 at 20:01






  • 2





    Putting in the number 3, for example, isn't a system exception but a program-defined boundary. Use an if-statement for that inside the try-block.

    – Drew Kennedy
    Nov 14 '18 at 20:01







  • 2





    You don't get an ArithmeticException from reading an int from a Scanner, as you are not doing any arithmetic. And a number is always either not equal to 1 or not equal to 2.

    – Andy Turner
    Nov 14 '18 at 20:02












  • It won't throw an exception for you, you need to do the logic and throw the exception.

    – SPlatten
    Nov 14 '18 at 20:03















1















The program I am making has a console-based text menu to select an option between 1 and 2, I need it to catch for any input that isnt a number, and isnt a number between 1 and 2. This is what I have



 Scanner scan = new Scanner(System.in);
int number = 0;
try
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();

catch (ArithmeticException e)
System.out.println("Arithmetic Exception");

catch (Exception e)
if (!(number == 1)


Any insight on where I have gone wrong is appreciated!










share|improve this question

















  • 2





    You say you’ve gone wrong - where does the actual behaviour differ from the intended?

    – MTCoster
    Nov 14 '18 at 20:00






  • 1





    What's the problem? You didn't specify.

    – NiVeR
    Nov 14 '18 at 20:01






  • 2





    Putting in the number 3, for example, isn't a system exception but a program-defined boundary. Use an if-statement for that inside the try-block.

    – Drew Kennedy
    Nov 14 '18 at 20:01







  • 2





    You don't get an ArithmeticException from reading an int from a Scanner, as you are not doing any arithmetic. And a number is always either not equal to 1 or not equal to 2.

    – Andy Turner
    Nov 14 '18 at 20:02












  • It won't throw an exception for you, you need to do the logic and throw the exception.

    – SPlatten
    Nov 14 '18 at 20:03













1












1








1








The program I am making has a console-based text menu to select an option between 1 and 2, I need it to catch for any input that isnt a number, and isnt a number between 1 and 2. This is what I have



 Scanner scan = new Scanner(System.in);
int number = 0;
try
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();

catch (ArithmeticException e)
System.out.println("Arithmetic Exception");

catch (Exception e)
if (!(number == 1)


Any insight on where I have gone wrong is appreciated!










share|improve this question














The program I am making has a console-based text menu to select an option between 1 and 2, I need it to catch for any input that isnt a number, and isnt a number between 1 and 2. This is what I have



 Scanner scan = new Scanner(System.in);
int number = 0;
try
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();

catch (ArithmeticException e)
System.out.println("Arithmetic Exception");

catch (Exception e)
if (!(number == 1)


Any insight on where I have gone wrong is appreciated!







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 19:59









Paul JonesPaul Jones

266




266







  • 2





    You say you’ve gone wrong - where does the actual behaviour differ from the intended?

    – MTCoster
    Nov 14 '18 at 20:00






  • 1





    What's the problem? You didn't specify.

    – NiVeR
    Nov 14 '18 at 20:01






  • 2





    Putting in the number 3, for example, isn't a system exception but a program-defined boundary. Use an if-statement for that inside the try-block.

    – Drew Kennedy
    Nov 14 '18 at 20:01







  • 2





    You don't get an ArithmeticException from reading an int from a Scanner, as you are not doing any arithmetic. And a number is always either not equal to 1 or not equal to 2.

    – Andy Turner
    Nov 14 '18 at 20:02












  • It won't throw an exception for you, you need to do the logic and throw the exception.

    – SPlatten
    Nov 14 '18 at 20:03












  • 2





    You say you’ve gone wrong - where does the actual behaviour differ from the intended?

    – MTCoster
    Nov 14 '18 at 20:00






  • 1





    What's the problem? You didn't specify.

    – NiVeR
    Nov 14 '18 at 20:01






  • 2





    Putting in the number 3, for example, isn't a system exception but a program-defined boundary. Use an if-statement for that inside the try-block.

    – Drew Kennedy
    Nov 14 '18 at 20:01







  • 2





    You don't get an ArithmeticException from reading an int from a Scanner, as you are not doing any arithmetic. And a number is always either not equal to 1 or not equal to 2.

    – Andy Turner
    Nov 14 '18 at 20:02












  • It won't throw an exception for you, you need to do the logic and throw the exception.

    – SPlatten
    Nov 14 '18 at 20:03







2




2





You say you’ve gone wrong - where does the actual behaviour differ from the intended?

– MTCoster
Nov 14 '18 at 20:00





You say you’ve gone wrong - where does the actual behaviour differ from the intended?

– MTCoster
Nov 14 '18 at 20:00




1




1





What's the problem? You didn't specify.

– NiVeR
Nov 14 '18 at 20:01





What's the problem? You didn't specify.

– NiVeR
Nov 14 '18 at 20:01




2




2





Putting in the number 3, for example, isn't a system exception but a program-defined boundary. Use an if-statement for that inside the try-block.

– Drew Kennedy
Nov 14 '18 at 20:01






Putting in the number 3, for example, isn't a system exception but a program-defined boundary. Use an if-statement for that inside the try-block.

– Drew Kennedy
Nov 14 '18 at 20:01





2




2





You don't get an ArithmeticException from reading an int from a Scanner, as you are not doing any arithmetic. And a number is always either not equal to 1 or not equal to 2.

– Andy Turner
Nov 14 '18 at 20:02






You don't get an ArithmeticException from reading an int from a Scanner, as you are not doing any arithmetic. And a number is always either not equal to 1 or not equal to 2.

– Andy Turner
Nov 14 '18 at 20:02














It won't throw an exception for you, you need to do the logic and throw the exception.

– SPlatten
Nov 14 '18 at 20:03





It won't throw an exception for you, you need to do the logic and throw the exception.

– SPlatten
Nov 14 '18 at 20:03












3 Answers
3






active

oldest

votes


















1














If you want to throw an exception, this is easy but take care with the condition:



Scanner scan = new Scanner(System.in);
int number = 0;
try
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();
if ((number != 1) && (number != 2))
throw new Exception();


catch (InputMismatchException e)
System.out.println("This is not a number");

catch (Exception e)
System.out.println("Inside here because the number is not 1 or 2");



The condition



(number != 1) && (number != 2)


is true if number is not (1 or 2)






share|improve this answer
































    0














     Scanner scan = new Scanner(System.in);
    int number = 0;
    try
    System.out.println("Enter a number ");
    System.out.println("1.");
    System.out.println("2.");
    number = scan.nextInt();
    if (number != 1 && number != 2)
    throw new Exception();


    catch (ArithmeticException e)
    System.out.println("Arithmetic Exception");

    catch (Exception e)
    System.out.println("Inside here because the number is not 1 or 2 ");






    share|improve this answer
































      0














      As a suggestion, you can use



      if ((number != 1) || (number != 2))



      instead of



      if (!(number == 1) || !(number == 2))






      share|improve this answer




















      • 1





        Sure, but why not just use if (true), that's equivalent and simpler again.

        – Andy Turner
        Nov 14 '18 at 20:15










      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307911%2ftry-catch-exception-help-in-java%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      If you want to throw an exception, this is easy but take care with the condition:



      Scanner scan = new Scanner(System.in);
      int number = 0;
      try
      System.out.println("Enter a number ");
      System.out.println("1.");
      System.out.println("2.");
      number = scan.nextInt();
      if ((number != 1) && (number != 2))
      throw new Exception();


      catch (InputMismatchException e)
      System.out.println("This is not a number");

      catch (Exception e)
      System.out.println("Inside here because the number is not 1 or 2");



      The condition



      (number != 1) && (number != 2)


      is true if number is not (1 or 2)






      share|improve this answer





























        1














        If you want to throw an exception, this is easy but take care with the condition:



        Scanner scan = new Scanner(System.in);
        int number = 0;
        try
        System.out.println("Enter a number ");
        System.out.println("1.");
        System.out.println("2.");
        number = scan.nextInt();
        if ((number != 1) && (number != 2))
        throw new Exception();


        catch (InputMismatchException e)
        System.out.println("This is not a number");

        catch (Exception e)
        System.out.println("Inside here because the number is not 1 or 2");



        The condition



        (number != 1) && (number != 2)


        is true if number is not (1 or 2)






        share|improve this answer



























          1












          1








          1







          If you want to throw an exception, this is easy but take care with the condition:



          Scanner scan = new Scanner(System.in);
          int number = 0;
          try
          System.out.println("Enter a number ");
          System.out.println("1.");
          System.out.println("2.");
          number = scan.nextInt();
          if ((number != 1) && (number != 2))
          throw new Exception();


          catch (InputMismatchException e)
          System.out.println("This is not a number");

          catch (Exception e)
          System.out.println("Inside here because the number is not 1 or 2");



          The condition



          (number != 1) && (number != 2)


          is true if number is not (1 or 2)






          share|improve this answer















          If you want to throw an exception, this is easy but take care with the condition:



          Scanner scan = new Scanner(System.in);
          int number = 0;
          try
          System.out.println("Enter a number ");
          System.out.println("1.");
          System.out.println("2.");
          number = scan.nextInt();
          if ((number != 1) && (number != 2))
          throw new Exception();


          catch (InputMismatchException e)
          System.out.println("This is not a number");

          catch (Exception e)
          System.out.println("Inside here because the number is not 1 or 2");



          The condition



          (number != 1) && (number != 2)


          is true if number is not (1 or 2)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 20:26

























          answered Nov 14 '18 at 20:15









          forpasforpas

          17.6k3728




          17.6k3728























              0














               Scanner scan = new Scanner(System.in);
              int number = 0;
              try
              System.out.println("Enter a number ");
              System.out.println("1.");
              System.out.println("2.");
              number = scan.nextInt();
              if (number != 1 && number != 2)
              throw new Exception();


              catch (ArithmeticException e)
              System.out.println("Arithmetic Exception");

              catch (Exception e)
              System.out.println("Inside here because the number is not 1 or 2 ");






              share|improve this answer





























                0














                 Scanner scan = new Scanner(System.in);
                int number = 0;
                try
                System.out.println("Enter a number ");
                System.out.println("1.");
                System.out.println("2.");
                number = scan.nextInt();
                if (number != 1 && number != 2)
                throw new Exception();


                catch (ArithmeticException e)
                System.out.println("Arithmetic Exception");

                catch (Exception e)
                System.out.println("Inside here because the number is not 1 or 2 ");






                share|improve this answer



























                  0












                  0








                  0







                   Scanner scan = new Scanner(System.in);
                  int number = 0;
                  try
                  System.out.println("Enter a number ");
                  System.out.println("1.");
                  System.out.println("2.");
                  number = scan.nextInt();
                  if (number != 1 && number != 2)
                  throw new Exception();


                  catch (ArithmeticException e)
                  System.out.println("Arithmetic Exception");

                  catch (Exception e)
                  System.out.println("Inside here because the number is not 1 or 2 ");






                  share|improve this answer















                   Scanner scan = new Scanner(System.in);
                  int number = 0;
                  try
                  System.out.println("Enter a number ");
                  System.out.println("1.");
                  System.out.println("2.");
                  number = scan.nextInt();
                  if (number != 1 && number != 2)
                  throw new Exception();


                  catch (ArithmeticException e)
                  System.out.println("Arithmetic Exception");

                  catch (Exception e)
                  System.out.println("Inside here because the number is not 1 or 2 ");







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 14 '18 at 23:45

























                  answered Nov 14 '18 at 20:11









                  Akeme UAkeme U

                  113




                  113





















                      0














                      As a suggestion, you can use



                      if ((number != 1) || (number != 2))



                      instead of



                      if (!(number == 1) || !(number == 2))






                      share|improve this answer




















                      • 1





                        Sure, but why not just use if (true), that's equivalent and simpler again.

                        – Andy Turner
                        Nov 14 '18 at 20:15















                      0














                      As a suggestion, you can use



                      if ((number != 1) || (number != 2))



                      instead of



                      if (!(number == 1) || !(number == 2))






                      share|improve this answer




















                      • 1





                        Sure, but why not just use if (true), that's equivalent and simpler again.

                        – Andy Turner
                        Nov 14 '18 at 20:15













                      0












                      0








                      0







                      As a suggestion, you can use



                      if ((number != 1) || (number != 2))



                      instead of



                      if (!(number == 1) || !(number == 2))






                      share|improve this answer















                      As a suggestion, you can use



                      if ((number != 1) || (number != 2))



                      instead of



                      if (!(number == 1) || !(number == 2))







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 15 '18 at 5:53









                      Rahul Sharma

                      2,4241819




                      2,4241819










                      answered Nov 14 '18 at 20:09









                      kamilyrbkamilyrb

                      187




                      187







                      • 1





                        Sure, but why not just use if (true), that's equivalent and simpler again.

                        – Andy Turner
                        Nov 14 '18 at 20:15












                      • 1





                        Sure, but why not just use if (true), that's equivalent and simpler again.

                        – Andy Turner
                        Nov 14 '18 at 20:15







                      1




                      1





                      Sure, but why not just use if (true), that's equivalent and simpler again.

                      – Andy Turner
                      Nov 14 '18 at 20:15





                      Sure, but why not just use if (true), that's equivalent and simpler again.

                      – Andy Turner
                      Nov 14 '18 at 20:15

















                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307911%2ftry-catch-exception-help-in-java%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo