Java异常处理实例讲解

发表于:2018-6-08 09:24

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:小楼夜听雨QAQ    来源:Linux社区

#
java
#
Java
分享:
  在Java这种纯面向对象编程语言中,有着“一切皆对象”的说法。所以,java也是通过类来描述程序出现的异常现象。在编译的时候,一些严重的错误,比如说语法错误,会被虚拟机检测出来。而异常指的是程序运行过程中,受到了一些阻碍,无法继续进行下去。这个时候就会抛出异常,从当前位置向上级一层一层地抛出。
  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;
      }
  }

  发现:如果没有使用try catch语句,出现问题时,后面的代码都无法执行。如果加上try catch语句,那么语句块以外的还能够继续执行,但是语句块内部的代码,从出现问题的地方开始,都不能执行。
  那么普通的代码块可以屏蔽这个错误,使得代码块外面的继续执行吗,见下面代码:
  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;
      }
  }

  结果是不可以,看来只有 try catch语句块可以。
  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),我们将立即处理。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号