(异常的产生)把下面代码补充完整 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 16:05:08 浏览: 207
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("类型转换出错");
}
}
}
相关问题
写出下面程序标记为1~6的执行顺行 public class UsingException { public static void main(String[] args) { try { method1();//1 } catch(Exception e) { System.out.println(e.getMessage());//2 } finally { System.out.println("Program is end!");//3 } } public static void method1()throws Exception{ method2();//4 } public static void method2()throws Exception{ method3();//5 } public static void method3()throws Exception{ throw new Exception("Exception thrown in method3");//6 } }
1、method1();2、e.getMessage();3、"Program is end!";4、method2();5、method3();6、"Exception thrown in method3"。
public class Test { public static void main(String args[]){ try { System.out.println("1:calling the method"); method(); System.out.println("2:After the method call"); } catch (RuntimeException
e) { System.out.println("3:Exception caught in main method"); } } public static void method() { try { System.out.println("4:Calling the method"); throw new RuntimeException(); } catch (RuntimeException e) { System.out.println("5:Exception caught in method"); throw e; } finally { System.out.println("6:Finally block executed"); } } }
请问,运行以上代码会输出什么结果?
阅读全文