title: Java Exceptions Prevent Closing The Program (when clicking X button)
body: <p>I've built a little random number generator using JOptionPane. The exceptions that I've written prevent the user from quitting the program when clicking the X button. I've done lots of research and tried several things but nothing seems to work.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>import javax.swing.JOptionPane;
  import java.util.Random;
  
  public class Generate {
      private int number;
      private int min;
      private int max;
      private int repeat;
      Random no = new Random();
      int x = 1;
  
      void generateNumber() {
          do {
              try {
                  String from = (String) JOptionPane.showInputDialog(null, "Welcome to Random Number Generator!\n\nPlease insert your number range.\n\nFrom:", "Random Number Generator", JOptionPane.QUESTION_MESSAGE, null, null, "Enter Number");
                  min = Integer.parseInt(from);
  
                  String to = (String) JOptionPane.showInputDialog(null, "To:", "Random Number Generator", JOptionPane.QUESTION_MESSAGE, null, null, "Enter Number");
                  max = Integer.parseInt(to);
                  System.out.println();
  
                  String count = (String) JOptionPane.showInputDialog(null, "How many numbers would you like?", "Random Number Generator", JOptionPane.QUESTION_MESSAGE, null, null, "Enter Number");
                  repeat = Integer.parseInt(count);
                  System.out.println();
  
                  for (int counter = 1; counter &lt;= repeat; counter++) {
                      number = no.nextInt(max - min + 1) + min;
                      JOptionPane.showMessageDialog(null, "Random number #" + counter + ": " + number, "Random Number Generator", JOptionPane.PLAIN_MESSAGE);
                  }
                 x = 2;
              } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null, "INPUT ERROR: please insert a number", "Random Number Generator", JOptionPane.ERROR_MESSAGE);
              } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "INPUT ERROR: the second number needs to be higher than the first", "Random Number Generator", JOptionPane.ERROR_MESSAGE);
                  }
              } while(x == 1);
          }
  }
  </code></pre>
  
  <p>Main: </p>
  
  <pre><code>  class RandomNumber {
      public static void main(String[] args) {
          Generate obj = new Generate();
          obj.generateNumber();
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/InYsv.gif" rel="nofollow noreferrer">This is what happens when I try to close the program</a></p>