java 异常操作
定义一个自定义异常类 InputCheckException ,当用户2次输入数据内容不一致时,抛出并捕获该异常
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//定义一个自定义异常类 InputCheckException ,当用户2次输入数据内容不一致时,抛出并捕获该异常
class InputCheckException extends Exception {
@Override
public String getMessage()
{
return "两次输入的数据不一致";
}
}
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
boolean flag=true;
do
{
System.out.println("输入密码:");
String pass1=br.readLine();
System.out.println("再次输入密码:");
String pass2=br.readLine();
try
{
if (!pass1.equals(pass2))
{
throw new InputCheckException();
}
flag=false;
System.out.println("2次密码一致");
}
catch (InputCheckException e)
{
System.out.println(e.getMessage());
}
} while (flag);
}
浙公网安备 33010602011771号