设为首页
收藏本站
切换到宽版
登录
立即注册
找回密码
搜索
搜索
本版
帖子
用户
快捷导航
论坛
BBS
VIP用户组
官网群
无名商城论坛
»
论坛
›
资源分享区
›
学习资源专区
›
【HC】Java中的异常处理
返回列表
发帖
查看:
415
|
回复:
0
[其他技术]
【HC】Java中的异常处理
[复制链接]
无名
无名
当前离线
积分
32464
1万
主题
1万
帖子
3万
积分
管理员
积分
32464
发消息
发表于 2022-5-8 18:41:24
|
显示全部楼层
|
阅读模式
Java中的异常处理
在这里插入图片描述示例1:public static void main(String[] args) { try { System.out.println(3/0); } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("finally"); }}123456789结果:
在这里插入图片描述示例:Multi-catchpublic static void main(String[] args) { int d = new Scanner(System.in).nextInt(); try { System.out.println(3 / d); new File("G:/dd/aa/ab.txt").createNewFile(); } catch (ArithmeticException e) { e.printStackTrace(); } catch (IOException e) { // System.out.println(e.getMessage()); e.printStackTrace(); } //可以简写为 try { System.out.println(3 / d); new File("G:/dd/aa/ab.txt").createNewFile(); } catch (ArithmeticException | IOException e) { e.printStackTrace(); }}12345678910111213141516171819示例:实现一:抛出异常 public static void main(String[] args) throws IOException { File file = new File("pom.xml"); FileReader reader = new FileReader(file); char[] buf = new char[128]; int len = -1; while((len = reader.read(buf))!= -1){ String res = new String(buf, 0, len); System.out.println(res); } reader.close(); }1234567891011实现二:捕获异常public static void main(String[] args) { File file = new File("pom.xml"); FileReader reader = null; try { reader = new FileReader(file); char[] buf = new char[128]; int len = -1; while ((len = reader.read(buf)) != -1) { String res = new String(buf, 0, len); System.out.println(res); } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } }}12345678910111213141516171819202
printStackTrace
,
String
,
reader
,
System
,
File
相关帖子
•
破解易验证教程
•
【Box】Java零基础1,小白教程,大佬勿喷
•
【FUT】8个python实用脚本
•
如何让你手机防止root锁机
•
【FUT】Java代码分享-打印九九乘法表
•
【FUT】检测逆向
•
【FUT】代码分享—iapp获取QQ昵称
•
【Max】团队 教大家如何查看QQ昵称
回复
使用道具
举报
返回列表
发帖
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
立即注册
本版积分规则
发表回复
回帖后跳转到最后一页
快速回复
返回顶部
返回列表