Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the exception types and handling methods of Java?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Java exception types and handling methods are what, for this problem, this article details the corresponding analysis and solutions, hoping to help more small partners who want to solve this problem to find a simpler and easier way.

The abnormal structure is:

Throwable is the top-level parent

Error is a serious error,

Exception is what we call an exception.

I. Keywords for exception handling

Java has five keywords for exception handling: try, catch, finally, throw, and throws.

throw throw exception, throws declare exception, catch exception try_catch

1、throwpublic class SegmentFault { public static void main(String[] args) { /** * throw throw exception * format- throw new exception class name (parameter); * */ //create an array int [] arr = { 2, 4, 56 ,5}; //Find the corresponding element according to the index int index = 4; int element = getElement(arr,index); System.out.println(element); System.out.println("owo"); //run error cannot continue } /** throw throws an exception to remind you that you must handle */ public static int getElement(int [] arr, int index){ //determine whether the array index is out of bounds if (index

< 0 || index >

arr.length -1){ /** * 条件满足越界 当执行到throw抛出异常后就无法运行,结束方法并且提示 * */ throw new ArrayIndexOutOfBoundsException("数组下标越界异常"); } int element = arr[index]; return element; }}

异常结果为:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 数组下标越界异常

2、throwspublic class SegmentFault{ public static void main(String [] args){ read("a.txt"); } public static void read(String path) throws FileNotFoundException, IOException { if (!path.equals("a.txt")){ // 如果没有a.txt // 如果不是 a.txt 该文件不存在 是一个错误 也就是异常 throw throw new FileNotFoundException("文件不存在"); } if (!path.equals("b.txt")){ throw new IOException("文件不存在"); } } }

异常结果为:

Exception in thread "main" java.io.IOException: 文件不存在

try、catch、finally + Throwable中的常用方法。

Throwable常用方法如下:

printStackTrace() : *打印异常详细信息。

getMessage() : 获取异常原因。

toString(): 获取异常类型及描述信息。

public class Demo03 { public static void main(String[] args) { /** * try- catch 捕获异常 * */ // 可能会生成的异常 try { // 捕获或者声明 read("b.txt"); } catch (FileNotFoundException e) { // 使用某种捕获,实现对异常的处理 System.out.println(e); /** * Throwable中的查看方法 * getMessage 获取异常信息 提示给用户看的 * toString 获取异常的类型和异常描述(不用) * printStackTrace * */ System.out.println("Throwable常用方法测试"); System.out.println(e.getMessage()); // 文件不存在 System.out.println(e.toString()); e.printStackTrace(); } finally { System.out.println("不管程序怎样,这里都会被执行"); } System.out.println("over"); } public static void read(String path) throws FileNotFoundException { if (!path.equals("a.txt")) { throw new FileNotFoundException("文件不存在"); } } }

输出结果为:

java.io.FileNotFoundException: 文件不存在

-----Throwable常用方法测试------

文件不存在

java.io.FileNotFoundException: 文件不存在

不管程序怎样,这里都会被执行

注意事项 :try、 catch、 finally、都不可以单独使用

关于Java异常类型及处理方法是怎样的问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report