好得很程序员自学网

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

Java学习笔记之异常处理

本文实例为大家分享了java 异常处理 的具体代码,供大家参考,具体内容如下

一.异常的分类

1.由java虚拟机抛出的异常(error):程序无法处理的问题,用户不用去进行处理(虚拟机错误丶内存溢出错误丶线程死锁)

2.exception异常:程序本身可以进行处理的异常

1).非检查异常(unchecked exception):编译器不需要强制处理的异常(空指针异常丶数组下标越界异常丶算数异常丶类型转换异常)

2).检查异常(checked exception):编译器需要强制处理的异常(ioexception丶sqlexception) 

二.异常处理的两种方法

1.通过try丶catch和finally关键字在当前位置进行异常处理

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

public static void main(string[] a){

  int sum = 0 ;

  while ( true ){

   try {   //以两数相除除数不能为0进行举例

    system.out.println( "请依次输入两个数值进行除法操作:" );

    scanner scanner = new scanner(system.in);

    int one =scanner.nextint();

    int two =scanner.nextint();

    sum = one/two;

    system.out.println( "最终结果为:" +sum);

   } catch (exception e) {     //用catch将错误进行捕捉,这里可以使用多重catch,对于不同的错误进行捕捉,但最后的catch建议为exception。

    // todo auto-generated catch block //显示错误堆栈信息

    e.printstacktrace();                          

   } finally {            

 

  system.out.print( "无论有没有错误我都会执行" );          }

  }

  }

}

输出:

2.通过try丶catch丶finally丶throw和throws抛出异常给函数调用者进行处理

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

public class try {

  public static void main(string[] a){

   try {

    function();   //在函数调用者处对异常进行处理

   } catch (exception e)

   {

    e.printstacktrace();

   }

  }

  static void function() throws exception{   //通过throws将异常进行抛出

  

   system.out.println( "请输入一个数值进行判断:" );

   scanner scanner = new scanner(system.in);

   int one =scanner.nextint();

   if (one< 100 )

   {

    throw new exception();  //若输入的数值小于100则抛出异常

   }

  }

}

输出:

3.自定义异常进行处理

?

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

class myexception extends exception{   //自定义异常,通过super方法传递异常信息给父级

  public myexception(){

  super ( "这是我自定义的异常" );

  }

}

 

public class try {

  public static void main(string[] a){

   try {

    function();

   } catch (myexception e)

   {

    e.printstacktrace();

   }

  }

  static void function() throws myexception{

  

   system.out.println( "请输入一个数值进行判断:" );

   scanner scanner = new scanner(system.in);

   int one =scanner.nextint();

   if (one< 100 )

   {

    throw new myexception();  //将自定义异常进行抛出  

   }

  }

}

输出:

三.异常链

有的时候我们会在处理一个异常的时候抛出一个新的异常,也就是异常的嵌套,但是最后我们得到的异常信息却只有一个。

示例:

?

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

public class try {

  public static void main(string[] a){

   try {

    function1();

   } catch (exception e)

   {

    e.printstacktrace();

   }

  }

  static void function1() throws exception{

   try {

    function2();

   } catch (exception e){

    throw new exception();

   }

  }

  static void function2() throws exception{

   try {

    function3();

   } catch (exception e){

    throw new exception();

   }

  }

  static void function3() throws exception{

    throw new exception();

  }

}

输入结果:

这样的话显示出的异常就只有一个了,那我们如果想让这条异常链中的所有异常信息全部输出该怎么办呢?方法很简单,我们在抛出异常的时候将异常对象也当作参数进行抛出就行了。

示例:

?

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

public class try {

  public static void main(string[] a){

   try {

    function1();

   } catch (exception e)

   {

    e.printstacktrace();

   }

  }

  static void function1() throws exception{

   try {

    function2();

   } catch (exception e){

    throw new exception( "异常2" ,e);

   }

  }

  static void function2() throws exception{

   try {

    function3();

   } catch (exception e){

    throw new exception( "异常2" ,e);

   }

  }

  static void function3() throws exception{

    throw new exception( "异常3" );

  }

}

运行结果:

到此,我们java中的异常便是描述完了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://www.cnblogs.com/ygxdbmx/archive/2018/10/03/9736189.html

查看更多关于Java学习笔记之异常处理的详细内容...

  阅读:12次