Java异常处理实例如何详细解析和深入理解?

2026-04-19 14:461阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计561个文字,预计阅读时间需要3分钟。

Java异常处理实例如何详细解析和深入理解?

1. 异常例子 class TestTryCatch { public static void main(String[] args) { int[] arr=new int[5]; arr[7]=10; System.out.println(end!!!); } } 输出: (越界)Exception in thread main java.lang.ArrayIndexOutOfBoundsException: 7

1. 异常例子

class TestTryCatch { public static void main(String[] args){ int arr[] = new int[5]; arr[7] = 10; System.out.println("end!!!"); } }

输出:(越界)

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at TestTryCatch.main(TestTryCatch.java:4) 进程已结束,退出代码1

2. 异常处理


class TestTryCatch { public static void main(String[] args){ try { int arr[] = new int[5]; arr[7] = 10; } catch (ArrayIndexOutOfBoundsException e){ System.out.println("数组范围越界!"); System.out.println("异常:"+e); } finally { System.out.println("一定会执行finally语句块"); } System.out.println("end!!!"); } }

输出:

数组范围越界! 异常:java.lang.ArrayIndexOutOfBoundsException: 7 一定会执行finally语句块 end!!!

3. 抛出异常

语法:throw 异常类实例对象;

int a = 5, b = 0; try{ if(b == 0) throw new ArithmeticException("一个算术异常,除数0"); else System.out.println(a+"/"+b+"="+ a/b); } catch(ArithmeticException e){ System.out.println("抛出异常:"+e); }

输出:

抛出异常:java.lang.ArithmeticException: 一个算术异常,除数0

对方法进行异常抛出

void add(int a, int b) throws Exception { int c = a/b; System.out.println(a+"/"+b+"="+c); }

TestTryCatch obj = new TestTryCatch(); obj.add(4, 0);

输出:(报错)

Java异常处理实例如何详细解析和深入理解?

java: 未报告的异常错误java.lang.Exception; 必须对其进行捕获或声明以便抛出

可见,方法后面跟了 throws 异常1, 异常2...,则 必须 在调用处 处理

改为:

TestTryCatch obj = new TestTryCatch(); try{ obj.add(4, 0); } catch (Exception e){ System.out.println("必须处理异常:"+e); }

输出:

必须处理异常:java.lang.ArithmeticException: / by zero

4. 编写异常类

语法:(继承 extends Exception 类)

class 异常类名 extends Exception{ ...... }

class MyException extends Exception{ public MyException(String msg){ // 调用 Exception 类的构造方法,存入异常信息 super(msg); } }

try{ throw new MyException("自定义异常!"); } catch (Exception e){ System.out.println(e); }

输出:

MyException: 自定义异常!

到此这篇关于Java异常处理实例详解的文章就介绍到这了,更多相关Java异常处理内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计561个文字,预计阅读时间需要3分钟。

Java异常处理实例如何详细解析和深入理解?

1. 异常例子 class TestTryCatch { public static void main(String[] args) { int[] arr=new int[5]; arr[7]=10; System.out.println(end!!!); } } 输出: (越界)Exception in thread main java.lang.ArrayIndexOutOfBoundsException: 7

1. 异常例子

class TestTryCatch { public static void main(String[] args){ int arr[] = new int[5]; arr[7] = 10; System.out.println("end!!!"); } }

输出:(越界)

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at TestTryCatch.main(TestTryCatch.java:4) 进程已结束,退出代码1

2. 异常处理


class TestTryCatch { public static void main(String[] args){ try { int arr[] = new int[5]; arr[7] = 10; } catch (ArrayIndexOutOfBoundsException e){ System.out.println("数组范围越界!"); System.out.println("异常:"+e); } finally { System.out.println("一定会执行finally语句块"); } System.out.println("end!!!"); } }

输出:

数组范围越界! 异常:java.lang.ArrayIndexOutOfBoundsException: 7 一定会执行finally语句块 end!!!

3. 抛出异常

语法:throw 异常类实例对象;

int a = 5, b = 0; try{ if(b == 0) throw new ArithmeticException("一个算术异常,除数0"); else System.out.println(a+"/"+b+"="+ a/b); } catch(ArithmeticException e){ System.out.println("抛出异常:"+e); }

输出:

抛出异常:java.lang.ArithmeticException: 一个算术异常,除数0

对方法进行异常抛出

void add(int a, int b) throws Exception { int c = a/b; System.out.println(a+"/"+b+"="+c); }

TestTryCatch obj = new TestTryCatch(); obj.add(4, 0);

输出:(报错)

Java异常处理实例如何详细解析和深入理解?

java: 未报告的异常错误java.lang.Exception; 必须对其进行捕获或声明以便抛出

可见,方法后面跟了 throws 异常1, 异常2...,则 必须 在调用处 处理

改为:

TestTryCatch obj = new TestTryCatch(); try{ obj.add(4, 0); } catch (Exception e){ System.out.println("必须处理异常:"+e); }

输出:

必须处理异常:java.lang.ArithmeticException: / by zero

4. 编写异常类

语法:(继承 extends Exception 类)

class 异常类名 extends Exception{ ...... }

class MyException extends Exception{ public MyException(String msg){ // 调用 Exception 类的构造方法,存入异常信息 super(msg); } }

try{ throw new MyException("自定义异常!"); } catch (Exception e){ System.out.println(e); }

输出:

MyException: 自定义异常!

到此这篇关于Java异常处理实例详解的文章就介绍到这了,更多相关Java异常处理内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!