package step1;
import java.util.Scanner;
public class ThrowException {
public static void function(String id)throws IllegalArgumentException { //利用throws关键字声明该方法可能出现异常IllegalArgumentException
/********** Begin *********/
if(id.length()==7)
{
System.out.println("ID格式正确:"+id);
}
else throw new IllegalArgumentException("ID长度应为7");
/********** End *********/
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String id=sc.nextLine();
try {
function(id);
}
catch (IllegalArgumentException e) {
/********** Begin *********/
System.out.println(e.getMessage());
/********** End *********/
}
}
}
Educoder -Java面向对象 - Java语言之异常处理类(第1关:了解异常 - ID检测)
最新推荐文章于 2023-11-24 18:30:25 发布