LESSON 26A   Simple example of handling an error

 

import javax.swing.JOptionPane;
public class registrar

{

private boolean error = false;

public registrar()

{

student stu = new student();
stu.setName(JOptionPane.showInputDialog("Enter the student's name"));

do
{
try
{
stu.setAge(Integer.parseInt(JOptionPane.showInputDialog("Enter the student's age")));
error = false;
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Please enter a number", "Error", 0);
error = true;
}
}
while(error == true);
}
public static void main(String args[])

{
registrar app = new registrar();
}
}