logo资料库

自定义异常.doc

第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
资料共6页,全文预览结束
《Java程序设计》实验报告四 班级:计算机科学与技术(1)班 学号: 姓名: 1 题目 自定义一个异常,编程创建并抛出,某个异常类的实例,运行该程序,并观察运行 结果。 2 目的和要求 用户密码的合法化验证,要求密码由4到6个数字组成,若长度不落在这个范围或不 是由数字组成,抛出自己的异常。 3 功能说明 输入用户名和密码,如果不在4-6位,输出“错误!输入的密码必须四到六位”,如 果不全是由字母组成,输出“错误!输入的密码必须均为数字”。如果正确,输出用户名和 密码。 4 设计流程图 5 源程序 import java.awt.*; import java.applet.*; import java.awt.event.*; public class ActionListener{ Password extends Applet implements
Label prompt0; Label prompt[]=new Label[2]; TextField input[]=new TextField[2]; String id; Button btn; public void init(){ prompt0=new Label("请输入密码:"); prompt[0]=new Label("用户名"); prompt[1]=new Label("密 码"); this.setSize(500, 250); setLayout(null); prompt0.setFont(new Font("Serief",Font.BOLD,18)); prompt0.setSize(200, 30); prompt0.setLocation(150, 50); add(prompt0); prompt0.setBackground(Color.YELLOW); for(int i=0;i<2;i++){ input[i]=new TextField(); prompt[i].setSize(50, 20); prompt[i].setBackground(Color.YELLOW); input[i].setSize(150, 20); prompt[i].setLocation(150, 100+25*i); input[i].setLocation(205, 100+25*i); add(prompt[i]); add(input[i]); input[i].addActionListener(this); } input[1].setEchoChar('*'); btn =new Button("确定"); btn.setSize(50, 20); btn.setLocation(230, 170); add(btn); btn.addActionListener(this); } private static final long serialVersionUID = 1L; int { PassWordException,PassWordException2 Inputpw(String str)throws
//注意参数设置 if(!(str.length()<=6&&str.length()>=4)){ input[0].setText("错误!输入的密码必须四到六位"); throw(new } for(int i=0;i='0'&&str.charAt(i)<='9')) PassWordException(str)); input[0].setText("错误!输入的密码必须均为数字"); throw(new PassWordException2(str)); } String name =input[0].getText(); input[0].setText("输入密 码正确! 用户名:"+name+" 密码: { } "+str); return 0; } public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="确定"){ prompt[i].setVisible(false); for(int i=0;i<2;i++){ } input[1].setVisible(false); btn.setLabel("返回"); prompt0.setText("系统密码检验:"); input[0].setSize(300, 50); input[0].setLocation(150, 100); id=new String(input[1].getText()); try{ } catch(PassWordException e1){ Inputpw(id); } ---------------------"); System.out.println("----------------- 异 常 处 理 System.out.println(e1.getMessage()); System.out.println(e1.toString()); } catch(Exception e1) { System.out.println("----------------- 异 常 处 理
---------------------"); System.out.println(e1.getMessage()); System.out.println(e1.toString()); } System.out.println("----------异常处理后请重新输入密 码-----------"); for(int i=0;i<2;i++){ if(e.getActionCommand()=="返回"){ prompt[i].setVisible(true); input[i].setText(""); } input[1].setVisible(true); btn.setLabel("确定"); prompt0.setText("请输入密码: "); input[0].setSize(150, 20); input[0].setLocation(205, 100); } } //自定义异常 class PassWordException extends Exception{ } /** * */ private static final long serialVersionUID = 1L; private String password; public PassWordException(String word){ } public String toString(){ password=word; if(!(password.length()<=6&&password.length()>=4)){ return("错误!请输入四到六位数的密码"); return(null); } } class PassWordException2 extends Exception{ } private static final long serialVersionUID = 1L; private String password; public PassWordException2(String word){
password=word; } public String toString(){ for(int i=0;i='0' && password.charAt(i)<='9')) return("错误!输入的密码必须均为数字"); } return(null); } } 6 数据结果
7 实验心得与体会
分享到:
收藏