Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- /*-*-*-*-*-*-*-*-*--*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
- Create an applet which takes number from text field and on clicking button labelled
- "read" checks whether the number is in range of 0 to 100 or not. If not it should
- throw customized exception "Out of Range...". If exception is thrown, put message
- "Enter Number within range 0 to 100" in text field.
- -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
- import java.applet.Applet;
- import java.awt.Button;
- import java.awt.TextField;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- /* <applet code = "CheckNumber" width= 400 height = 400>
- </applet>
- */
- public class CheckNumber extends Applet implements ActionListener
- {
- TextField t1;
- Button b1;
- public void init()
- {
- t1 = new TextField(50);
- add(t1);
- b1 = new Button("Read");
- add(b1);
- b1.addActionListener(this);
- }
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource() == b1)
- {
- try
- {
- check(Integer.parseInt(t1.getText()));
- t1.setText(t1.getText() + " OK...");
- }
- catch(NoOutOfRange ex)
- {
- t1.setText("Enter number bewteen 0 to 100...");
- }
- catch(Exception ex)
- {
- t1.setText("Invalid Number");
- }
- }
- }
- static void check(int val) throws NoOutOfRange
- {
- if(val < 0 || val > 100)
- {
- throw new NoOutOfRange("Range");
- }
- }
- }
- class NoOutOfRange extends Exception
- {
- String msg;
- NoOutOfRange(String s)
- {
- msg = s;
- }
- public String toString()
- {
- return("No. Out Of Range : " + msg );
- }
- }

Post new comment