Exception

1,编译时异常和运行时异常的分类

 *

 * 运行时异常:RuntimeException 以及 所有的子类都是运行时异常,

 *    特点:编译时不发生,运行的时候,有可能发生。

 *   

 *    常见的运行时异常:

 *       ArithmeticException 算术异常

 *       ArrayIndexOutOfBoundsException 数组越界异常

 *       NullPointerException 空指针异常

 *       ClassCastException 类型转换异常 多态的时候发生

 *       

 *    处理方案:

 *    

 * 编译时异常:Exception 以及子类,

 * 但是不包括   RuntimeException 以及子类的其他异常类型。

 *   特点:编译的时候发生,并且,不修改代码,程序无法编译通过,程序更加无法运行。

 *  

 *   常见的编译时异常:

 *  

 *  IOException

 *  SQLException

 *  FileNotFoundException

 *  CloneNotSupportedException

 *  ClassNotFoundException

 *     

 *   处理方案:

 *      1.throws,

 *      2.try...

5,Throws关键字的使用

在声明一个方法的时候使用的

作用:就是告诉那些调用本方法程序,我这个方法可能会抛出某些类型的异常,以便调用该方法的程序做出相应的处理

我们现在根据一个例子程序来讲解这个问题,我尽量给大家讲解详细一些;

package draw;

import java.util.Scanner;

 

public class Sub{


 public static void main(String[] agrs ){
  try {
   divide();      //
2.我们在mian方法中调用下面抛出异常的方法,这个时候我们用try-catch语句进行处理
  } catch (Exception e) {
   System.out.println("
除数不能为零");
   e.printStackTrace();    ///
3.这个方法的意思打印出异常信息,大家可以在调试这个程序的时候去掉这句话试试效果
  }
 }


 public static void divide() 
throws Exception{   ///1.这里我们就抛出了一个Exception的异常,我们把这个放申明为static是为了举例的方便,不用创建对象就可以调用它。


  Scanner input = new Scanner(System.in);
  System.out.println("
请输入被除数:");
  int num = input.nextInt();
  System.out.println("
请输入除数:");
  int num2 = input.nextInt();
  
  System.out.println("num
除以num2的商是:"+num/num2);
 }
}

以上就是一个抛出异常--调用方法的时候捕获异常的一般过程

3,演示Throwable 中的常用方法

 *

 * getMessage(): 返回对异常的描述信息

 *

 * toString():获取异常类名和异常信息,返回字符串。

 *

 * printStackTrace() :获取异常类名和异常信息,以及异常出现在程序中的位置。返回值void。

 *    也就是栈的调用信息

练习:

*/

public classExceptionDemo3 {

 

      public static void main(String[]args)  {

           // TODO Auto-generated method stub

 

          

           int[] array = new int[]{1,2,3};

          

           String[] array1 = newString[]{"jerry","susuan"};

          

           try {

                 method1();

           } catch (Throwable ex) {

                 // TODO Auto-generated catchblock

//              ex.printStackTrace();

                 System.out.println(ex.getMessage());

                

                 System.out.println(ex.toString());

                

                 //根据方法的调用的返回值类型,自动产生局部变量的定义。的快捷键ctrl+2,l

                 StackTraceElement[] stackTrace= ex.getStackTrace();

                

                 for(inti=0;i<stackTrace.length;i++){

                     

                      StackTraceElement element= stackTrace[i];

                     

                      System.out.println(element.getClassName());

                      System.out.println(element.getFileName());

                      System.out.println(element.getMethodName());

                      System.out.println(element.getLineNumber());

                     

                      System.out.println(element.toString());

                 }

           }

 

      }

     

      public static void method1() throwsThrowable{

           method2();

      }

 

      private static void method2() throwsThrowable {

           Throwable   throwable =

                      new Throwable("故意的,创建的一个异常对象");

          

          

           int i=10;

           if(i>0){

                 throw throwable;

           }

          

      }

 

}

4,finally语句块:

*/

public classFinallyDemo {

 

      public static void main(String[] args) {

//        method1();

           int result = method2();

           System.out.println("result ="+result);

      }

 

      public static int method2() {

           int result = 0;

          

           int[] array = null;

          

           try {

                 array = new int[]{1,2,3};

                

                 System.out.println(array[3]);

                

                

           } catch (Exception e) {

                 e.printStackTrace();

                

                 return 10;

           }

           finally{

                 result = array.length;

                 System.out.println("finally语句块被执行了");

           }

 

           return result;

      }

 

      public static void method1() {

           // 局部变量的定义和声明部分

           int[] array = null;// 引用类型 用null来初始化

           Scanner sc = null;

           int i = 0; // 基本数据类型,用0来初始化

 

           try {

                 array = new int[] { 1, 2, 3 };

 

                 sc = new Scanner(System.in);

 

                 System.out.println(array[3]);

 

                 // 当上面的代码 中有异常发生的时候,该语句被跳过,不会被执行

                 // System.out.println("数组的长度:"+array.length);

 

                 // sc.close();

 

           } catch (Exception e) {

                 // 记录异常

                 e.printStackTrace();

           } finally {

                 // finally 语句块,有一个特点,不论是否发生异常,finally中的语句,都会执行到。

                 System.out.println("数组的长度:" + array.length);

 

                 // 系统资源的释放

                 sc.close();

           }

           System.out.println("helloworld");

      }

 

}

 

 

 

 

 

6

,importjava.io.IOException;

 

public class X {

      public static void start() {

           System.out.println("异常测试");

      }

 

      public static void main(String args[]) {

           try {

                 start();

           } catch (IOException ioe) {

                 ioe.printStackTrace();

           }

      }

}

 

上面的例子中的错误解释:

6,java中存在除0 异常,但是0.0(是double类型)并不是0,所以除于0.0并不报错,而计算负数的平方根会得到NaN

控制台输出Infinity正无穷大

7,

 

1)演示异常:

 * A:除0,://java.lang.ArithmeticException:表示异常

     //java.lang它所在的包名

异常发生的时候,jvm默认的处理流程

      * 1,把发生的程序的执行过程(方法调用的栈信息),显示到控制台

      * 2,终端程序的继续执行

 * B:数组越界:

      *

 * C:空指针:

什么时候发生空指针异常:

      * 在对象的值为null的对象上面,调用了对象的任何成员,

      * 就会抛出空指针异常

      *java.lang.NullPointerException

      * */

C:throws也是处理异常的方法

D:try,catch也是处理异常的方法

/try catch语句块的执行顺序为

   /*

    * 程序按顺序执行,try语句块中的代码,当执行的语句,有异常发生的时候

    * 例如:算数异常ArithmeticException

    * 那么程序会转到catch语句块部分继续执行,并且会携带,刚好有算数异常ArithmeticException的的分支,就

    * 执行该分支下的代码,执行完就不再执行其他catch携带的代码块,而是执行非tey catch的输出语句。

    *

    * catch语句块可以写多个,多个语句块之间可以是并行的,也可以是继承关系,只不过

    * 要先写子类,父类必须写后面。

E:jdk1.7关于异常处理的新特性

例如:Scanner的关闭资源写法:

A:写法一sc.close();

B:写法二:

 

//写法二

     try(Scanner sc1=new Scanner(System.in)){//自动帮资源回收

2)编程原则:

 * A:小步快跑

 *B:永远不要相信别人传递过来的参数

尽量给所有的代码,增加注释,并且添加try catch

 * */

17,数组的异常:

数组索引越界
 *   ArrayIndexOutOfBoundsException
 *   
 *  发生的时机:访问到了数组中的不存在的索引时发生。
 *   Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
      at com.newedu.jb.day05.ArrayDemo5.main(ArrayDemo5.java:20)
                包名.类名.方法名(文件名 : 发生行号)
             */
 
 *  
 *  空指针异常
 *    NullPointerException
 *    Exception in thread "main" java.lang.NullPointerException
      at com.newedu.jb.day05.ArrayDemo5.main(ArrayDemo5.java:50)
             */
 
 *   发生的时机:数组名(引用)没有指向堆内存(实体),却在操作(堆内存)实体中的元素时
 

1,        从网页上面抓取数据,并且存到数据库里面。例如fro循环抓取前50页的数据

如多con.close,pstmt.close,rs.close,没有关,会出现异常

Data source rejected establishment ofconnection,  message from server:"Too many connections"

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值