(异常的产生)把下面代码补充完整 public class TestThrow{ public static void main(String args[]){ throwException(10); } public static void throwException(int n){ if (n == 0){ //抛出一个 NullPointerException }else{ //抛出一个 ClassCastException //并设定详细信息为“类型转换出错” } } } (异常的产生)把下面代码补充完整 public class TestThrow{ public static void main(String args[]){ throwException(10); } public static void throwException(int n){ if (n == 0){ //抛出一个 NullPointerException }else{ //抛出一个 ClassCastException //并设定详细信息为“类型转换出错” } } }
时间: 2023-05-24 18:05:08 浏览: 163
Java throw Exception实现异常转换
5星 · 资源好评率100%
public class TestThrow{
public static void main(String args[]){
try{
throwException(10);
} catch(NullPointerException e){
System.out.println("发生了NullPointerException异常");
} catch(ClassCastException e){
System.out.println("发生了ClassCastException异常:类型转换出错");
}
}
public static void throwException(int n){
if (n == 0){
throw new NullPointerException("空指针异常");
}else{
throw new ClassCastException("类型转换出错");
}
}
}
阅读全文