title: Try Catch With Loop Java
body: <p>I'm new Java student.
  Maybe I don't understand how try/catch  really works.
  I'm making a Java class game, the hangman.
  And I'm making a method that returns the number of players.
  I have another code that works perfectly. Just if I introduce letters and no Int, it crash. I tried to make this one.
  I hope you understand me.</p>
  
  <pre><code>public static int setNumJugadores() {
      Scanner sc = new Scanner(System.in);
      int numJugador=0;
      System.out.println("Introduzca el número de jugadores a jugar: ");
      // VARIABLES PARA EL TRY CATCH
  
  
      boolean bError=false;
      boolean mayorQueCero=false;
      do {
          try{
              numJugador = sc.nextInt();
          }
          catch (Exception e){
              bError=true;
              System.out.println("Error, introduzca un numero entero.");
          }
  
          if (numJugador &lt; 1) {
              System.out.println("ERROR, introduzca un valor valido mayor de 0");
          }
          else{
              mayorQueCero=true;
          }
      } while ((!mayorQueCero)||(!bError));
  
      return numJugador;
  }       
  </code></pre>