Exception in Java

What is exception?

exception is subclass of throwable.

exception(异常)又分为checked exception and unchecked exception.俗称意外情况。

checked exception 是除runtime exception之外的exception.可以被解决的exception.

unchecked exception是runtime exception 和error.无法被解决的exception.

如果不加以干涉,代码运行至exception会自动throw出原因并且退出程序.

int a = 10;
int b = 0;
System.out.println(a/b);
System.out.println("Hello");

如果不加以干涉,则代码运行到第三行时,throw出exception并且System.exit();

如何干涉exception

单个exception

需要用以下代码格式进行干涉。

try{
"你将要干涉的代码"
}catch(exception名称){
"代码(how to do)"
}
finally{
}

用上面代码做例子。

int a = 10;
int b = 0;
try{
System.out.println(a/b);}
catch(ArithmeticException e){// Arithmetic e = new Arithmetic();
System.out.println("除数为0 无法运算");
}
System.out.println("Hello");

则结果为

"除数为0 无法运算"
"Hello"

多个exception

int a =10;
int b = 0;
String str =null;
int[] list = new int[3];
try{
System.out.println(a/b);//System.exit();
list[10]= 100;
System.out.println(str.length());}
catch(ArithmeticException e){
System.out.println("除数为0");}
catch(NullPointerException e){
System.out.println("空指针异常");}
catch(ArrayIndexOfOutBoundsException e){
System.out.println("数组下标越界");}

但是在运行中会发现,在try中第一行出现了exception后,catch到ArithmeticException后输出“除数为0”便结束了运行,无法进行try中第一行后代码的运行。

需要注意的是,一一列举出来的exception是定向查找,如果需要最后找到未知的exception,便可以在最后加一个catch。并且不能放到所有定向查找Exception的前面,只能最后用Superclass兜底。并用printStackTrace打印出相关吗名称。

int a =10;
int b = 0;
String str =null;
int[] list = new int[3];
try{
System.out.println(a/b);//System.exit();
list[10]= 100;
System.out.println(str.length());}
catch(ArithmeticException e){
System.out.println("除数为0");}
catch(NullPointerException e){
System.out.println("空指针异常");}
catch(ArrayIndexOfOutBoundsException e){
System.out.println("数组下标越界");}
catch(Exception e){
System.out.println("出现Exception");
e.printStackTrace();}//写出具体的Exception名称

 Throws

上面都是runtime exception,可以用try catch finally进行干涉。如果是complie exception则是通过throws,将所有方法中的异常抛出,并交给引用此方法的method进行修改。(不处理异常)

String time = "2024-07-17";
SimpleDataFormat sdf = new SimpleDataFormat("yyyy-mm-dd");
sdf.parse(time);//会出现编译Exception

第一种可以用try catch进行干涉。

第二种便可以用throws进行exception的抛出。

如例,如果不进行抛出,则cal方法无法被调用,无法complie。

public static void main(String[] args){
cal();
}
public static int cal() throws ArithmeticException{
int a = 10;
int b = 0;
int result = a/b;
return result;
}

则将会在main函数中cal()报错,在main中进行干涉,则可以用try catch 在main中进行干涉。throws可以同时抛出无数个exception。

如果是编译时异常,throws之后必须在调用的method中将其解决,否则无法运行。(谁调用谁处理) 

Override一个方法时,throws的异常范围不能被扩大。例如

public interface A{
    void class() throws Exception;
}
public static void B(){
    void class() throws NullPointerException{//is ok
}
}

public interface A{
    void class() throws NullPointerException;
}
public static void B(){
    void class() throws Exception{// not ok
}
}

Throw

public static void main(String[] args){
cal();
}
public static int cal(){
int a = 10;
int b = 0;
if(b == 0){
throw new ArithmeticException("除数为零不能运算");}
int result = a/b;
return result;
}

如果方法中有throw 并且出现异常,但是调用此方法的method无法知道是否会有异常,为了信息对等,则需要在有throw的方法中加入throws。

public static void main(String[] args){
cal();
}
public static int cal() throws ArithmeticException{
int a = 10;
int b = 0;
if(b == 0){
throw new ArithmeticException("除数为零不能运算");}
int result = a/b;
return result;
}

Throws和Throw的区别

1.throws用在方法名后,可以throws多个异常,并且为异常class名,用逗号隔开;throw用在方法体内,每次只能throw一个异常,为异常对象(new)。

2.throws表示将异常抛出,交给调用的方法解决;throw表示将异常抛出交给方法内的语句解决。

3.throws表示可能发生的情况,并不代表一定发生。throw抛出异常对象。

Finally

finally代码在try代码执行之后一定会执行(除了try中有return或者System.exit())。

public static void main(String[] args){
test();}
public static int test(){
    int x =1 ;
    try{
x++;
return x;}
finally{
x++;
System.out.println(x);}
}

输出结果为 3 2 .

Finally 和 Final 、finalize的区别

final修饰的类不能被继承,修饰的方法不能被重写,修饰的量为常量

finally为try catch中的一部分,常用于资源释放。

finalize是Object类中一个方法,用于垃圾回收。

自定义异常

常见异常是throwable,exception,runtimeexception的子类,如果我们需要一个异常并不在规定子类中,则需要自定义一个异常。

public class ScoreException extends Exception{
public ScoreException(){
}
public ScoreException(String str){
System.out.println(str);}}

假如现在我们要自定义一个分数必须在0-100之间的异常。

public static double getScore(double score) throws ScoreException{
double score = score;
if(score<0 && score >100){
    throw new ScoreException("分数不合法")}
return score;}

便可以在分数不合法时抛出exception。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值