好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

新手了解java 异常处理基础知识

一、异常体系结构

1.什么是异常

在java程序运行过程中,发生了一些意料之外的情况就是异常。在java中异常一颗分为两大类:

(错误)Error 和 (异常)Exception。

对于(错误)Error来说,我们无法通过程序去解决所有的错误,仅仅可以去尝试捕获这些错误,但是无法处理,往往错误的发生对程序来说是重大的致命性问题,需要通过较大的调整去修正它。 对于(异常)Exception来说,它是我们可以通过程序去处理的,我们可以对这些异常情况做出捕获和处理。

2.异常的体系结构

​ Throwable

​ Error Exception

​ RutimeException 编译时异常

说明: RutimeException 和一些编译时异常继承了 Exception , Exception 和 Error 继承了 Throwable 。

运行时异常( RutimeException )在代码中我们往往不需要去捕获它,而是通过处理代码的逻辑来规避运行时异常。常见的5种运行时异常:

ClassCastException (类转换异常) ArrayIndexOutOfBoundsException (数组越界异常) NullPointerException (空指针) ArrayStoreException (数据存储异常,操作数组时类型不一致) NumberFormatException 数字格式化异常

?

1

2

3

4

5

6

7

8

9

10

11

12

public class Demo{

     public void test(){

         //此代码声明这里会抛出一个运行时异常

        throw new RutimeException();

     }

}

public class DemoTest{

     public static void main(String[] args){

        Demo demo = new Demo()

            demo.test();

     }

}

从上述代码种可以看出,运行时异常产生时,代码可以正常的编译与运行,但是运行结果会抛出一个运行时异常。

说明:运行时异常会向上传播。

二、异常处理

当异常发生时,我们需要对其进行处理,处理异常有两种方式:

一种使用try…catch…finally进行捕获;

另一种使用throws的方式进行抛出;

try…catch…finally

语法

?

1

2

3

4

5

6

7

try {

// 可能发生异常的代码块

} catch ( /* 需要捕获的异常类 */ ) {

// 捕获到异常后的处理代码块

} finally {

// 不管是否会发生异常,此代码块都会被执行

}

示例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

public class Demo1{

     public static void main(String[] args){

         String[] strs = { "张三" , "李四" , "王五" };

         try {

             System.out.println( "-------" );

             System.out.println(strs[ 3 ]); // 此处会发生异常,当异常发生后,后面的代码都不会被执行到

                 for (String str : strs) {

                     System.out.println(str);

                 }

         } catch (ArrayIndexOutOfBoundsException e) {

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

         } catch (Exception e) {

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

         } finally {

             System.out.println( "--------------" );

         }

     }

}

说明:在try…catch…语句中,try语句中产生异常的代码后面的代码不会被执行,产生异常后直接到了catch语句块中。catch语句块可以写多个,catch语句分支是从上到下依次找对应类型的,但是注意catch捕获的异常类型中父类型必须写在子类型的后面。

finally语句块是无论异常是否产生都会执行的。常用与资源的释放。

throw 与 throws

throws是异常处理机制的一种,而throw不是; throws是声明异常类;而throw是抛出异常对象; throws可以声明多个异常类,而throw只能抛出一个异常对象; throws是声明在方法上的,而throw是定义在方法内的;

示例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

public class Demo{

     public void test() throws Exception{

         //此代码声明这里会抛出一个异常

        throw new Exception();

     }

}

public class DemoTest{

     public static void main(String[] args){

        Demo demo = new Demo()

          try {

            demo.test();

          } catch (Exception e) {

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

         } finally {

             System.out.println( "--------------" );

         }

     }

}

三、自定义异常

运行时异常

继承 RutimeException

示例:

?

1

2

3

4

5

public class MyRutimeException extends RutimeException {

     public MyRutimeException(String message){

         super (message);

     }

}

编译时异常

继承Exception

示例:

?

1

2

3

4

5

public class MyException extends Exception {

     public MyException(String message){

         super (message);

     }

}

总结

本篇文章就到这里了,希望对你有所帮助,也希望您能够多多关注的更多内容!

原文链接:https://blog.csdn.net/ww741258963123/article/details/117670481

查看更多关于新手了解java 异常处理基础知识的详细内容...

  阅读:14次