public class Demo {
public static void main(String[] args) {
int i = 10;
int n = 0;
try {
//业务代码
//因为,这一行代码被0除,所以,下一行代码不会被执行
int res = i/n;
//永远不会执行
System.out.println(res);
}catch(ArithmeticException e){
//e表示ArithmeticException类的局部变量
System.out.println("不能被0除");
}
}
}
6.2.3.2 可以使用两种方式输出异常信息
获取异常的描述信息
public class Demo {
public static void main(String[] args) {
int i = 10;
int n = 0;
try {
//业务代码
//因为,这一行代码被0除,所以,下一行代码不会被执行
int res = i/n;
//永远不会执行
System.out.println(res);
}catch(ArithmeticException e){
//e表示ArithmeticException类的局部变量
String message = e.getMessage();
System.out.println("异常的描述信息:"+message);
}
}
}
获取异常堆栈信息(适合程序调试阶段)
public class Demo {
public static void main(String[] args) {
int i = 10;
int n = 0;
try {
//业务代码
//因为,这一行代码被0除,所以,下一行代码不会被执行
int res = i/n;
//永远不会执行
System.out.println(res);
}catch(ArithmeticException e){
//e表示ArithmeticException类的局部变量
e.printStackTrace();
}
}
}
6.2.3.3 受控异常
public class Demo {
public static void main(String[] args) {
try{
FileInputStream fileInputStream = new FileInputStream();
}catch (FileNotFoundException e){//必须显示的处理,因为,这是一个编译期异常
e.printStackTrace();
}
}
}
6.2.3.4 finally关键字
特点:无论执行是否正常,都必须执行的代码段
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Demo {
public static void main(String[] args) {
try{
FileInputStream fileInputStream = new FileInputStream("D:/a/a.txt");
}catch (FileNotFoundException e){//必须显示的处理,因为,这是一个编译期异常
e.printStackTrace();
}finally {
System.out.println("是必须执行的代码");
}
}
}
public class Demo {
public static void main(String[] args) {
int result = method();
System.out.println(result);
}
public static int method(){
int a = 10;
try{
return a;
}finally {
System.out.println("在return前执行");
a = 100;
}
}
}
深入finally;返回100
public class Demo {
public static void main(String[] args) {
int result = method();
System.out.println(result);
}
public static int method(){
int a = 10;
try{
a = 200;
}finally {
System.out.println("在return前执行");
a = 100;
}
return a;
}
}
6.2.3.5 final、finally、finalize的区别?
final:
修饰类不能被继承
修饰参数:
基本类型:值不能修改
引用类型:地址(对象)不能修改
static final修饰常量
finally:代码无论如何都必须执行
finalize():垃圾回收前调用的方法
6.3 如何声明异常
在方法中可以使用throws声明异常,如果声明的异常为受控异常,那么,在调用方法的时候必须处理
6.3.1 声明异常;处理异常
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Demo {
public static void main(String[] args) {
//调用方法
try {
method();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void method() throws FileNotFoundException{//声明异常
FileInputStream fileInputStream = new FileInputStream("d:/a.txt");
}
}
6.3.2 还是使用声明异常(不推荐)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Demo {
//可以在此声明异常,但是,这样就把异常处理交给java虚拟机处理了
public static void main(String[] args) throws FileNotFoundException {
//调用方法
method();
}
public static void method() throws FileNotFoundException{//声明异常
FileInputStream fileInputStream = new FileInputStream("d:/a.txt");
}
}
6.3.3 声明非受控异常
public class Demo {
public static void main(String[] args) {
//可以不编写try-catch,因为是个非受控异常
//method();
//建议编写异常处理
try{
method();
}catch (ArithmeticException e){
e.printStackTrace();
}
}
//非受控异常
public static void method() throws ArithmeticException{//声明异常
int i = 10,n = 0;
int r = i/0;
System.out.println(r);
}
}
6.4 手动抛出异常
使用throw关键字抛出异常
相当于return,以下的代码不被执行
可以抛出受控异常或非受控异常
6.4.1 自定义一般性异常
public class Demo {
public static void main(String[] args) {
//测试
method(1,0);
}
public static void method(int i,int n) {
try{
if (n==0){
//手动抛出
throw new MyException("不能为0");
}
if (!(n > 0 && n<=100)){
//手动抛出
throw new MyException("必须在1-100之间");
}
int result = i / n;
System.out.println("------------>>"+result);
}catch(Exception e){
e.printStackTrace();
}
}
}
//自定义异常类
class MyException extends Exception{
public MyException() {
super();
}
public MyException(String message) {
super(message);
}
public MyException(String message, Throwable cause) {
super(message, cause);
}
public MyException(Throwable cause) {
super(cause);
}
protected MyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
6.4.2 自定义运行期异常
public class Demo {
public static void main(String[] args) {
//测试
method(1,0);
}
//手动抛出异常的测试方法
public static void method(int i,int n) {
if (n==0){
//手动抛出
throw new MyException("不能为0");
}
if (!(n > 0 && n<=100)){
//手动抛出
throw new MyException("必须在1-100之间");
}
int result = i / n;
System.out.println("------------>>"+result);
}
}
//自定义异常类
class MyException extends RuntimeException{
public MyException() {
super();
}
public MyException(String message) {
super(message);
}
public MyException(String message, Throwable cause) {
super(message, cause);
}
public MyException(Throwable cause) {
super(cause);
}
protected MyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}