1、先看一个简单例子:
package myException; public class Demo_1 { public static void main(String[]args) { Test test1=new Test(); int num = test1.div(2,0); System.out.println(num); System.out.println("over......"); } } class Test { public int div(int a, int b){16 return a / b; } 18} |
如果程序在进行整除运算时,不小心除了0,会发生什么现象?
抛出了算术异常,程序立刻终止,因为最后一句“over......”也没有打印出来。2、try catch语句
既然有了异常,我们就要对其进行处理。
package myException; public class Demo_1 { public static void main(String[]args) { Test test1=new Test(); try { int num = test1.div(2,0); System.out.println(num); System.out.println("exception......"); }catch(ArithmeticException e) { e.printStackTrace(); } System.out.println("over......"); } } class Test { public int div(int a, int b){ return a / b; } } |
那么普通的代码块可以屏蔽这个错误,使得代码块外面的继续执行吗,见下面代码:
package myException; public class Demo_1 { public static void main(String[]args) throws ArithmeticException{ Test test1=new Test(); { int num = test1.div(2,0); System.out.println(num); System.out.println("over......"); } System.out.println("happy"); } } class Test { public int div(int a, int b) { return a / b; } } |
3、throw和throws
package myException; public class Demo_1 { public static void main(String[]args) throws ArithmeticException{ Test test1=new Test(); int num = test1.div(2,0); System.out.println(num); System.out.println("over......"); } } class Test { public int div(int a, int b) throws ArithmeticException{ if(b == 0) throw new ArithmeticException(); return a / b; } } |
基本的用法就是这样的,但是有几个注意点:
(1)throw方法体的内部,后面必须加切切实实new出来的对象,而throws用在方法名之后,不进入函数体,并且后面可以跟一个或多个类名(异常类),逗号隔开。
(2)如果一直向上抛出,而不catch,则会把异常抛给java处理机。
4、RuntimeException的特殊性
RuntimeException的子类有很多,比如上面的ArithmeticException,还有一些常见的比如IndexOutOfBoundsException(数组下标越界),NullPointerException(空指针异常)。为什么说它特殊呢?这种类型的异常通常都是由代码不当引起的,所以出现该异常时,希望程序停止,让维护人员进行代码的修改。所以在函数体内抛出的异常,函数名上是不需要申明的:
class Test { public int div(int a, int b) { if(b == 0) throw new ArithmeticException(); return a / b; } } |
所以这样写也是合法的。当然,函数名后面加上“throws ArithmeticException”也是可以的。
如果是非RuntimeException或者其子类的,必须处理,或者逐层抛出再处理。比如进行文件操作,或者连接数据库的时候:
进行某些操作时,必须抛出异常,或者处理异常。比如上述代码如果这样写,编译都不会通过,eclipse报错“Unhandle Exception type ClassNotFoundException”。5、Throwable类
在学习自定义异常类之前,先学习一下Throwable类。从字面上理解,“可以抛”的类,Throwable是所有异常类的基类,他的直接子类有Error类,Exception类。Throwable中的类主要是一些可以打印异常信息的方法,比如说 getMessage(), printStackTrace().他的子类可以根据具体情况覆盖重写这些方法。
6、自定义异常类
class DefineException extends Exception{ public DefineException() { } public DefineException(String str) { super(str); } } |
一般的格式就是这样,细节可以修改修改,可以根据实际情况覆盖重写Exception或者Throwable类的方法。当然,自定义的异常类必须要继承Throwable或者Exception。因为要想和throw、throws关键字搭配使用,必须是Throwable及其子类。
7、finally
finally一般和try catch相配合,在finally里处理一些必须要做、特别重要的信息,即使异常发生,finally里面的函数还是会被执行。特别是读写文件后的关闭、连接数据库的断开,比如下面的代码:
package com.ph; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class Test1 { public static void main(String[]args) { PreparedStatement ps=null; Connection ct=null; ResultSet rs=null; try { //1.加载驱动 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //2.得到链接 127.0.0.1:1433 ct=DriverManager.getConnection ("jdbc:sqlserver://127.0.0.1:1433;databaseName=Mytest","sa","123456"); ps=ct.prepareStatement("select * from course"); rs=ps.executeQuery(); while(rs.next()) { String cno=rs.getString(1); String cname=rs.getString(2); String tno=rs.getString(3); System.out.println("cno "+cno+" cname "+cname+" tno "+tno); } }catch(Exception e) { e.printStackTrace(); }finally { try { if(rs != null) rs.close(); if(ps != null) ps.close(); if(ct != null) ct.close(); }catch(Exception e) { e.printStackTrace(); } } } } |
即使程序出错,也可以保证断开数据库连接,释放所占用的数据库资源,防止资源浪费。
上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。