异常处理
package ZYL.inherit;
import java.util.*;
public class ExceptionTest
{
public static void main(String[] args)
{
try
{
Scanner in=new Scanner(System.in);
int x,y,z;
System.out.print("请输入两个数:");
x=in.nextInt();
y=in.nextInt();
z=x/y;
System.out.print("结果是:");
System.out.println(z);
}
catch(ArithmeticException e)
{
e.printStackTrace();
}
finally
{
System.out.println(1314);
}
}
}
packageZYL.inherit;
class Person
{
private String name;
private int age;
private String id;
public String getId()
{
return id;
}
public void setId(String id) throws IllegalArgumentException
{
if(id.length()!=18)
{
throw(new IllegalArgumentException());
}
this.id = id;
}
}
class IllegalArgumentException extends Exception
{
}
public class ExceptionTest1 {
public static void main(String[] args)
{
Person p1 = new Person();
Person p2 = new Person();
try
{
p1.setId("430602200012132513");
p2.setId("811561561");
}
catch (IllegalArgumentException e)
{
System.out.println("身份证长度有错误");
}
}
}