JAVA项目中的经常使用的异常处理状况

一、Error

LinkageError:连接错误;

ThreadDeath:线程死锁;

OutOfMemoryError:内存溢出;

StackOverflowError :堆栈溢出;

NoClassDefFoundError:类定义错误;

Virtual MachineError:虚拟机运行错误。

二、运行时异常(unChecked异常)

SecurityException:安全性异常;

NullPointerException:空指针异常;

ClassCastException:类型强制转换异常;

ClassNotFoundException:找不到类异常;

IllegalArgumentException:非法参数异常;

NegativeArraySizeException:数组长度为负异常;

ArithmeticException:算术条件异常。如:整数除零;

ArrayIndexOutOfBoundsException:数组下标越界异常;

ArrayStoreException:数组中包含不兼容的值抛出的异常;

StringIndexOutOfBoundsException:字符串下标越界异常;

ArrayStoreException:向数组中存放与声明类型不兼容对象异常;

三、非运行时异常(checked异常)

IOException:输入输出流异常;

SQLException:数据库操做异常;

EOFException:文件已结束异常;

TimeoutException:执行超时异常;

DataFormatException:数据格式化异常;

NoSuchFieldException:没有匹配的属性异常;

ClassNotFoundException:没有匹配的类异常;

FileNotFoundException:没有匹配的文件异常;

NoSuchMethodException:没有匹配的方法异常;

四、Throwable类的主要方法

public String getMessage():返回关于发生的异常的详细信息。这个消息在Throwable 类的构造函数中初始化了。

public Throwable getCause():返回一个Throwable 对象表明异常缘由。

public String toString():使用getMessage()的结果返回类的串级名字。

public void printStackTrace():打印toString()结果和栈层次到System.err,即错误输出流。

public StackTraceElement [] getStackTrace():返回一个包含堆栈层次的数组。下标为0的元素表明栈顶,最后一个元素表明方法调用堆栈的栈底。java

例子:sql

OutputStreamWriter out = ... 数据库

java.sql.Connection conn = ... 
try  // ⑸  
Statement stat = conn.createStatement();  
ResultSet rs = stat.executeQuery( "select uid, name from user" );  
while  (rs.next())  {  
     out.println( "ID:"  + rs.getString( "uid" // ⑹
     + ",姓名:"  + rs.getString( "name" ));  
     }  
     conn.close();  // ⑶  
     out.close(); 
catch (Exception ex)  // ⑵ 
{  
ex.printStackTrace();  //⑴,⑷ 
}
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
OutputStreamWriter out = ... java.sql.Connection conn = ...
      try  {
            Statement stat = conn.createStatement();
               ResultSet rs = stat.executeQuery( "select uid, name from user" );
                 while  (rs.next()){
                        out.println( "ID:"  + rs.getString( "uid" ) +  ",姓名: "  +
                   rs.getString( "name" ));
                 }
           catch (SQLException sqlex) {
                    out.println( "警告:数据不完整" );
                    throw  new  ApplicationException( "读取数据时出现SQL错误" , sqlex);
              catch (IOException ioex) {
                       hrow  new  ApplicationException( "写入数据时出现IO错误" , ioex);
                finally {
                            if  (conn !=  null ) {
                          try  {  conn.close();
                          catch (SQLException sqlex2) {
                              System.err( this .getClass().getName() + 
                       ".mymethod - 不能关闭数据库链接: "  + sqlex2.toString());                                       }
                           }
                          if  (out !=  null ) {
                              try  {
                               out.close();
                              } catch (IOException ioex2) {
                              System.err( this .getClass().getName() + 
                         ".mymethod - 不能关闭输出文件"  + ioex2.toString());
                                }
                           }
}

在 Java 应用程序中,异常处理机制为:抛出异常,捕捉异常。

抛出异常

  当一个方法出现错误引起异常时,方法建立异常对象并交付运行时系统,异常对象中包含了异常类型和异常出现时的程序状态等异常信息。运行时系统负责寻找处置异常的代码并执行。

  该方法的调用者必须处理或者从新抛出该异常。当方法的调用者无力处理该异常的时候,应该继续抛出,所经方法都层层上抛获取的异常,若最终都没有被处理,将交由虚拟机处理。处理也很简单,就是打印异常消息和堆栈信息,记录日志。

捕捉异常

  在方法抛出异常以后,运行时系统将转为寻找合适的异常处理器(exception handler)。潜在的异常处理器是异常发生时依次存留在调用栈中的方法的集合。当异常处理器所能处理的异常类型与方法抛出的异常类型相符时,即为合适的异常处理器。

  运行时系统从发生异常的方法开始,依次回查调用栈中的方法,直至找到含有合适异常处理器的方法并执行。当运行时系统遍历调用栈而未找到合适的异常处理器,若是出现异常的线程为主线程,则整个程序运行终止;若是非主线程,则终止该线程,其余线程继续运行。

  在方法中用try-catch语句捕获并处理异常,catach语句能够有多个,用来匹配处理异常。而且尽可能将捕获底层异常类的catch子句放在前面。

  异常老是先被抛出,后被捕捉的。

Java规定

  对于可查异常必须捕捉、或者声明抛出。容许忽略不可查的RuntimeException和Error。
  RuntimeException由业务逻辑保证。

、抛出异常实例(throws 和 throw)

public class Throws { public static void main(String[] args) throws Exception{//抛出异常类 System.out.println(10 / 0); throw new Exception("抛出异常对象"); //System.out.println("throw后面的代码再也不执行"); } }数组

(以上均从网上所找,并不是我的所写)安全

相关文章
相关标签/搜索