title: How do I get a specific error message for a thrown exception in Java?
body: <p>I'm trying to get two different messages for errors.  One to display: "X-Coordinate out of bounds", and the other "Please enter a number".  This is how I thought you'd write the code but, ofcourse, doesn't work.</p>
  
  <pre><code>int x = -1;
  int y = 0;
  
  while(x &lt; 0 || y &lt; 0)
  {
     try
     {
        System.out.println("X-Coordinate: ");
        x = kb.nextInt();
        if (x &lt; 0)
        {
           throw new Exception("X-Coordinate out of bounds"); // desired message
        }
        System.out.println("Y-Coordinate: ");
        y = kb.nextInt();
        if (y &lt; 0)
        {
           throw new Exception("Y-Coordinate out of bounds"); // desired message
        }
     }
     catch(Exception e)
     {
        // Desired default for other input errors
        System.out.println("Please enter a number"); 
        kb.nextLine();
     }
  }
  </code></pre>