class ExceptionTest
{
public static void main(String[] args)
{
Person p = new Person("张三", "不爱学java,爱玩游戏");
Person p1 = new Person("薛波", "喜欢java,而且很努力");
Heima.teachPerson(p);
Heima.teachPerson(p1);
}
}
class Person
{
private String name;
private String zt;
Person(String name, String zt)
{
this.name = name;
this.zt = zt;
}
public String getName()
{
return this.name;
}
public String getZt()
{
return this.zt;
}
}
class Heima
{
static void teachPerson(Person p)
{
try
{
if(p.getZt().contains("不爱学"))
throw new NoPersonException(p.getName()+"不属于黑马的招生对象,原因:", p.getZt());
System.out.println("黑马专业教学让"+p.getName()+"成为IT精英");
}
catch (NoPersonException e)
{
System.out.println(e.toString()+"此学生"+e.getZt());
}
System.out.println("谢谢你对黑马的关注。");
}
}
class NoPersonException extends Exception
{
private String zt;
NoPersonException(String msg, String zt)
{
super(msg);
this.zt = zt;
}
public String getZt()
{
return this.zt;
}
} |
|