他底下有两个子类:java.lang.Error与java.lang.Exception.咱们日常所说的异常,值的就是:java.lang.Exception.java
2法格式:数据库
Try{数组 //编译期间可能要出现异常的代码服务器 }catch(异常类型 异常对象){网络 /打印异常的代码jvm /打印异常的信息学习 }this |
解析:try: 该代码块中编写可能产生异常的代码。spa
Catch: 用来进行某种异常的捕获,实现对异常捕获的处理,线程
Try和catch 不能单独使用,必须结合使用。
public class Test0 { public static void main(String[] args) { int aa=10; int bb=0; try{ int cc=aa/bb; System.out.println(cc); }catch (ArithmeticException ce){ ce.printStackTrace(); } System.out.println("执行了吗??");
} } |
经过以上程序咱们发现,catch 只能处理try中所对应的异常类型,若是发生的异常和catch不匹配。则是没法处理的。所以try 后面能够有多个catch出现,每一个catch处理不一样的异常的类型。
public class Test0 { public static void main(String[] args) { int aa=10; int bb=0; int[]sum={1,2,3,4}; try{ int cc=aa/bb; //数组下标越界://java.lang.ArrayIndexOutOfBoundsException System.out.println(sum[4]); System.out.println(cc); }catch (ArithmeticException ce){ ce.printStackTrace(); }catch (ArrayIndexOutOfBoundsException ce){ ce.printStackTrace(); } System.out.println("执行了吗??");
} } |
Catch执行完后,代码的顺序是继续从Catch往下执行的,不会返回try语句块中,因此try..catch 会帮咱们捕获异常,可是会影响程序的正常的流程,最终咱们仍是要程序猿手动去处理发生的异常。
try{ System.out.println(10/0); }catch (ArrayIndexOutOfBoundsException ce){ ce.printStackTrace(); }finally { System.out.println("无论上面的异常是否执行,finall永远执行!!"); } } |
例如:
1.throw new ArithmeticException(“除数不能为零”); 2.throw new ArrayIndexOutOfBoundsException(“数组下标越界”); |
5.
/throw 抛出异常 public class Test2 { public static void main(String[] args) { int[]num={1,2,3,4,5,6,7}; int index=10; getSting(num,index); } public static void getSting(int[]num,int index){ //判断条件,若是知足,当执行完 throw 抛出异常对象后,方法已经没法继 续运行,这时就会结束当前方法的执行,并将其异常告知给调用者,这就是须要通 过异常来解决 if(index<0||index>num.length){ throw new ArrayIndexOutOfBoundsException("主人数组的下标太大的"); //System.out.println("ha"); //位于 throw new 下面的代码不会被执行,报错 }else{ System.out.println(num[index]); } } } //运行的结果:Exception in thread "main" // java.lang.ArrayIndexOutOfBoundsException: 主人数组的下标太大的 |
【注意】若是产生了异常,咱们就会将异常抛出,也就是将问题返回给该方法的调用者,那么对于调用者来讲,一种就是进行捕获处理,另外一种就是继续将异常抛出去,使用throw来声明。
[访问修饰符] 返回值类型 方法名(参数列表) throws 异常类名1….异常类2{ //代码块 } |
【注意】:throws只能用于方法的签名的后面,而throw 只能用于方法体中。
练习题:要求:
咱们模拟登陆操做,若是用户名已存在,则抛出异常并提示:该用户名已被注册。
a)首选自定义一个登陆异常类 LoginException
b)模拟登陆操做,使用数组或者 ArrayList 模拟数据库中存储的数据,而且提供当前注册帐号是否存在的方法用于判断。
//自定义一个异常类 public class LoginException extends Exception{ public LoginException() { }
public LoginException(String message) { super(message); }
public LoginException(String message, Throwable cause) { super(message, cause); }
public LoginException(Throwable cause) { super(cause); }
public LoginException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } }
public class Login { static String[]stuName={"liwenjie","zhangsan","李文杰","桑凤娇","郭朝旭"};
public static void main(String[] args){ Scanner input=new Scanner(System.in); System.out.println("请输入用户名"); String userName=input.next(); try{ boolean flag=display(userName); if(flag){ System.out.println("用户注册成功"); } }catch (Exception ce){ ce.printStackTrace(); } } public static boolean display(String name)throws Exception{ for(String ss:stuName){ if(ss.equals(name)){ //抛出一个异常 throw new LoginException("用户名已被注册过!!!"); } } return true; } }
//集合版的模拟登陆系统 public class Student{ public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("请输入用户名:"); String username=input.next(); try{ boolean flag=meth(username); if(flag){ System.out.println("注册成功"); } }catch (Exception ce){ ce.printStackTrace(); } } public static boolean meth(String name)throws Exception{ ArrayList<String>list=new ArrayList<>(); list.add("sangfengjiao"); list.add("liwnejie"); list.add("mashitian"); list.add("赵云"); list.add("zhangsan"); for(String ss:list){ if(ss.contains(name)){ throw new LoginException("用户名已被注册过!!!"); } } return true; } } |
2.6 3. 自定义异常类
每个学生(Student)都有学号,姓名和分数,分数永远不能为负数。若是老师给学生赋值一个负数,抛出一个自定义异常。
//自定义异常类 public class Student2 { private String stuName; private Integer stuXue; private Double stuScore; public Student2(){
} public Student2(String stuName,Integer stuXue,Double stuScore){ this.stuName=stuName; this.stuXue=stuXue; this.stuScore=stuScore; }
public String getStuName() { return stuName; }
public void setStuName(String stuName) { this.stuName = stuName; }
public Integer getStuXue() { return stuXue; }
public void setStuXue(Integer stuXue) { this.stuXue = stuXue; }
public Double getStuScore() { return stuScore; }
public void setStuScore(Double stuScore){ if(stuScore<0){ throw new StudentScore("分数不能为负数!!!!"); }else{ this.stuScore = stuScore; }
} } public class StudentScore extends RuntimeException{ public StudentScore() { }
public StudentScore(String message) { super(message); }
public StudentScore(String message, Throwable cause) { super(message, cause); }
public StudentScore(Throwable cause) { super(cause); }
public StudentScore(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } } public class Stundent3 { public static void main(String[] args) { //建立Student对象 Student2 ss=new Student2(); ss.setStuName("李文杰"); //抛出:day02.StudentScore: 分数不能为负数!!!! ss.setStuScore(-100.0); ss.setStuXue(33); System.out.println(ss.getStuName()); System.out.println(ss.getStuScore()); System.out.println(ss.getStuXue()); } } |