public class ExceptionTest {
public static void main(String[] args) {
int a = 10;
int b = 0;
int c = a / b;
// java.lang.ArithmeticException
System.out.println(c);
}
}
以上程序运行会出现异常信息,这个信息是JVM打印的。
Java中异常以类的形式存在:public class ExceptionTest {
public static void main(String[] args) {
// 通过异常类实例化异常对象
NumberFormatException exception1 = new NumberFormatException("数字格式化异常");
// java.lang.NumberFormatException: 数字格式化异常
System.out.println(exception1);
NullPointerException exception2 = new NullPointerException("空指针异常");
// java.lang.NullPointerException: 空指针异常
System.out.println(exception2);
}
异常的继承结构:Object下有Throwable(可抛出的)
Throwbale
--Error(不可处理,直接退出JVM)
--Exception(可处理的)
----Exception (编译时异常) 程序编译阶段需要对这些异常进行处理,如果不处理编译器报错
----RuntimeException(运行时异常) 可以预先处理,也可以不管
编译时异常(受检异常、受控异常)和运行时异常(未受检异常、非受控异常),都是发生在运行阶段,编译阶段异常是不会发生的。 编译时异常:因为编译时异常必须在编译阶段预先处理,如果不处理,编译器就会报错 异常处理的两种方式:- 在方法声明的位置上,使用throws关键字,抛给上一级
- 使用try..catch语句进行异常的捕捉
public class ExceptionTest {
public static void main(String[] args) {
// doSome方法声明位置上有:throws ClassNotFoundException
// 我们在调用doSome方法的时候就必须对这种异常进行预先处理
// 如果不处理 编译器救护报错
// Unhandled exception: java.lang.ClassNotFoundException
// doSome();
}
private static void doSome() throws ClassNotFoundException{
}
}
第一种处理方式,在方法声明的地方继续使用throws,继续上抛:public class ExceptionTest {
public static void main(String[] args) throws ClassNotFoundException{
doSome();
}
private static void doSome() throws ClassNotFoundException{
}
}
第二种方式,使用try..catch语句:public class ExceptionTest {
public static void main(String[] args){
try {
doSome();
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
private static void doSome() throws ClassNotFoundException{
System.out.println("do some");
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class ExceptionTest {
public static void main(String[] args){
System.out.println("main begin");
try {
m1();
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
}
System.out.println("main over");
}
private static void m1() throws FileNotFoundException {
System.out.println("m1 begin");
m2();
System.out.println("m1 over");
}
private static void m2() throws FileNotFoundException {
System.out.println("m2 begin");
m3();
System.out.println("m2 over");
}
// 这里调用了一个构造方法FileInputStream()
// 这个构造方法的声明位置上有: throws FileNotFoundException
// FileNotFoundException父类是IOException IOException父类是Exception
// 所以FileInputStream是编译时异常
private static void m3() throws FileNotFoundException {
new FileInputStream("DataTest");
}
}
异常对象常用的方法:import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class ExceptionTest {
public static void main(String[] args){
try {
FileInputStream file1 = new FileInputStream("DataTest");
} catch (FileNotFoundException e) {
// 获取异常简单的描述信息,这个信息实际上就是构造方法上面的String参数
System.out.println(e.getMessage());
// 打印异常堆栈信息,异步方式输出
e.printStackTrace();
System.out.println("鳄鱼君");
}
}
}
在finally子句中的代码块是最后执行的,并且是一定会执行的,即使try语句块中的代码出现了异常。finally子句必须和try一起出现,不能单独编写:import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ExceptionTest {
public static void main(String[] args){
FileInputStream file1 = null;
try {
// 创建输入流
file1 = new FileInputStream("DataTest");
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
// 流使用完需要关闭,流是占用资源的
if (file1 != null){
try {
// close方法有异常,采用捕捉的方式
file1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
try和finally可以组合使用:public class ExceptionTest {
public static void main(String[] args){
// try和finally可以组合使用
// try不能单独使用
// 先执行try,再执行finally,最后执行return
try {
System.out.println("鳄鱼君");
return;
} finally {
System.out.println("eyujun");
}
// 这里不能写语句
// System.out.println("永远无法执行");
}
}
public class ExceptionTest {
public static void main(String[] args){
try {
System.out.println("鳄鱼君");
// 退出JVM虚拟机,finally的语句就不会执行
System.exit(0);
} finally {
System.out.println("eyujun");
}
}
}
public class ExceptionTest {
public static void main(String[] args){
int i = m1();
System.out.println(i);//100
}
// 方法体中的代码,必须遵循自上而下顺序依次执行
// return语句一旦执行,整个方法必须结束
private static int m1() {
int i = 100;
try {
return i;
} finally {
i++;
}
}
}
声明:1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!如有侵权请邮件联系客服!
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!