FileDocCategorySizeDatePackage
ExceptionDemo1.javaAPI DocExample1803Mon Apr 02 14:34:32 BST 2001None

ExceptionDemo1.java

import java.awt.*;
import java.applet.Applet;
//import java.util.*;
import java.awt.event.*;
//import java.lang.Object;

public class ExceptionDemo1 extends Applet
    implements ActionListener {

    private TextField stringField;
    private TextField resultField;
    private Label resultLabel, stringLabel;



    public void init()  
    {
        stringLabel = new Label("Type an integer: ");
        resultLabel = new Label("Answer: ");
        stringField = new TextField(20);
        resultField = new TextField(20);
        resultField.setEditable(false);

        add(stringLabel);
        add(stringField);
        stringField.addActionListener(this);
        add(resultLabel);
        add(resultField);
    }

    public void actionPerformed(ActionEvent event) 
    {
        if (event.getSource() == stringField) 
        {
            try 
            {
                int number = Integer.parseInt(stringField.getText());
                checkNum(number);
                resultField.setText("Squared value is " + number*number);
            }
            catch (NumberFormatException e) 
            {
                resultField.setText("Error in number : Retype");
            }
            
            catch (myException me)
            {
	            resultField.setText("negative number: retype ");
            }
        }// endif    
     } // end actionPerformed
     
     public void checkNum(int n) throws myException
     {
     if (n < 0) throw new myException();
     }
     
     class myException extends Exception
     {
     	public myException() // constructor
     	{
          System.out.println("negative number exception");
     	}
     } // end myException
     
     
	}// end ExceptionDemo