抛出异常(新手)

博客提到目前处于懵懂状态,仅写出了相关写法,涉及Java、算法等内容,明天老师会进行详细讲解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个目前还是处于懵逼状态,只是把写法写出来,明天老师还会细讲。

==================================================

public class yichanglx {
public static void main(String[] args) throws Exception {//抛出异常你可以理解为在你做此项目的时候,你感觉这个地方有可能会出现异常, //可以写上抛出异常,到最后可以给你自己一个提示,也可以给别人一个提示。
int arr[] = {0,2};
getArray(arr);
}
private static int getArray(int[] arr)throws Exception {
if (arr.length==0){
throw new Exception("");
}
return 1;
}
}
=======================================================
public class zylx2 {
public static void main(String[] args){
int[] arr = null;
try{
int i = getArray(arr);
}catch (Exception e ){
System.out.println(e);
}
}
private static int getArray(int[] arr) throws Exception {
if (arr == null){
throw new NullPointerException("数组不存在。");
}if (arr.length<3){
throw new ArrayIndexOutOfBoundsException("数组没有3这个索引");
}
return arr[3]+1;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值