• 大小: 8KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-20
  • 语言: Java
  • 标签: java  异常机制  

资源简介

用户自行定义一个异常,编程创建并抛出某个异常类的实例,运行程序并观察执行结果 例如:用户密码的合法化验证,要求密码由4到六个数字组成,若长度不落在这个范围或不是由数字组成,抛出自己的异常

资源截图

代码片段和文件信息

/*
 * @(#)MyException.java 1.0 09/09/09
 *
 * You can modify the template of this file in the
 * directory ..\JCreator\Templates\Template_1\Project_Name.java
 *
 * You can also create your own project template by making a new
 * folder in the directory ..\JCreator\Template\. Use the other
 * templates as examples.
 *
 */
import java.io.*; 
import java.lang.*; 

class InputcodeException extends Exception{
          
     Code sourseCode;
     public InputcodeException(Code c)
     {
     super(“input error“);
     sourseCode=c;
     }
     public String toString()
     {
     return(“input the code error!“);
     }
    }
class Code
    {
     String ss;
     int i=0;

       
    
        public String checkcode()throws InputcodeException
        { 
          int l=ss.length();
          char[] s=ss.toCharArray(); 
                    
       if(l<4||l>6){             
        throw(new InputcodeException(this));
       }
      
       
       else
       {
        for(i=0;i        {
        if(s[i]<‘1‘||s[i]>‘9‘)
        throw(new InputcodeException(this));
        }
        return ss;
       }        
        }
        public void inputcode()
        {
      try{
          
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
         System.out.println(“输入密码:“);
         ss=br.readLine();
                
           }
          catch(IOException e){
      System.err.println(e);
        }                 
        }
       
    }
class MyException   {

public static void main(String args[]) {
Code c=new Code();    
     c.inputcode(); 
     try{
     System.out.print(“your code is “+c.checkcode());
     }
     catch(InputcodeException e){
     System.out.println(e.toString()); 
     }
}
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       1160  2009-11-18 22:31  MyException\classes\Code.class

     文件        410  2009-11-18 22:31  MyException\classes\InputcodeException.class

     文件        456  2009-11-08 19:46  MyException\classes\MyException$InputcodeException.class

     文件        444  2009-11-08 19:24  MyException\classes\MyException$TestcodeException.class

     文件        747  2009-11-18 22:31  MyException\classes\MyException.class

     文件       1326  2009-09-09 13:45  MyException\classes\myprojects\myexception\Code.class

     文件        479  2009-09-09 13:45  MyException\classes\myprojects\myexception\InputcodeException.class

     文件        612  2009-09-09 13:41  MyException\classes\myprojects\myexception\MyException$1.class

     文件        817  2009-09-09 13:45  MyException\classes\myprojects\myexception\MyException.class

     文件       1865  2009-09-09 14:30  MyException\MyException.java

     文件        797  2009-11-18 22:31  MyException\MyException.jcp

     文件        863  2009-11-18 22:31  MyException\MyException.jcu

     文件        302  2009-11-18 22:31  MyException\MyException.jcw

     文件       1616  2009-11-15 23:16  MyException\src\MyException.java

     文件          0  2009-11-18 22:31  MyException\src_myexception.txt

     目录          0  2009-11-09 16:00  MyException\classes\myprojects\myexception

     目录          0  2009-11-09 16:00  MyException\classes\myprojects

     目录          0  2009-11-15 22:32  MyException\classes

     目录          0  2009-11-15 23:16  MyException\src

     目录          0  2009-11-18 22:31  MyException

----------- ---------  ---------- -----  ----

                11894                    20


评论

共有 条评论