try { var n = prompt('Positive integer'); if(n < 0) throw('Please enter a positive integer'); } catch(e){ alert(e); }