9 Star 23 Fork 2

tzhanggit/JavaProgramming

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
TestEx.java 1.77 KB
一键复制 编辑 原始数据 按行查看 历史
TZhangOffice 提交于 2020-06-06 16:20 +08:00 . 2020春代码更新完毕
package ch6;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TestEx {
public static void main(String[] args){ // part5 main方法抛出异常 throws Exception,实际编程中不要写。
// Part 1:异常出现示例
int [] arr = {1, 2, 3};
System.out.println(arr[3]);
//System.out.println(2/0);
/*try {
System.out.println(2/0);
} catch(ArithmeticException ae) {
System.out.println("系统正在维护中,请与管理员联系"); // good or not
// ae.printStackTrace();
}*/
// Part 2:抛出异常示例
/*TestEx ex = new TestEx();
ex.m(0);*/
/*try {
new TextEx().m(0);
} catch(ArithmeticException e ) {
System.out.println("出错了");
}*/
// Part 3: 必须处理的异常继承于Exception
/*
FileInputStream in = null;
try {
in = new FileInputStream("test.txt");
int b;
b = in.read();
while(b != -1) {
System.out.print(b);
b = in.read();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
} */
// Part 4: try-catch-finally关系
TestEx te = new TestEx();
try {
new TestEx().f2();
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void m(int n) throws ArithmeticException {
if (n == 0) {
throw new ArithmeticException("被除数为0");
}
}
void f() throws FileNotFoundException, IOException {
FileInputStream in = new FileInputStream("test.txt");
int b;
b = in.read();
while( b != -1) {
System.out.println((char)b);
b = in.read();
}
}
void f2() throws FileNotFoundException, IOException { //处理,或者继续抛出
f();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/tzhanggit/JavaProgramming.git
git@gitee.com:tzhanggit/JavaProgramming.git
tzhanggit
JavaProgramming
JavaProgramming
master

搜索帮助