黑马程序员技术交流社区
标题:
求解运行结果
[打印本页]
作者:
吴凯
时间:
2013-6-24 23:33
标题:
求解运行结果
本帖最后由 孙百鑫 于 2013-6-27 07:33 编辑
class Demo
{
public static void func()
{
try
{
throw new Exception();
}
finally
{
System.out.println("B");
}
}
public static void main(String[] args)
{
try
{
func();
System.out.println("A");
}
catch(Exception e)
{
System.out.println("C");
}
System.out.println("D");
}
}
作者:
KaiM
时间:
2013-6-25 11:00
throw new Exception();就是告诉虚拟机要抛出异常,把他注释掉就行了。
作者:
majunm
时间:
2013-6-25 12:14
运行不通过 函数内部throw Exception 外部需要声明 throws Exception 改成throw RuntimeException() 运行结果 B C D
作者:
majunm
时间:
2013-6-25 12:16
class Demo
{
public static void func()
{
try
{
// System.out.println("f");
throw new RuntimeException();
}
finally
{
System.out.println("B");
}
}
public static void main(String[] args)
{
try
{
func();
System.out.println("A");
}
catch(Exception e)
{
System.out.println("C");
}
System.out.println("D");
}
}
复制代码
作者:
杨增坤
时间:
2013-6-25 17:53
public static void func()
throws Exception
{//因为此方法抛出了异常,所以要在此处获取异常,
try {
throw new Exception();
} finally {
System.out.println("B");
}
}
public static void main(String[] args) {
try {
func();
System.out.println("A");
} catch (Exception e) {
System.out.println("C");
}
System.out.println("D");
}
}
结果是:
BCD
只要抛出了异常,在try{}中抛出了异常的后的代码不再执行,如果捕获了异常,那么执行catch{}中的,最后在try{}外边的。希望对你有帮助!
作者:
刘_鑫
时间:
2013-6-25 18:08
运行报错了,gunc方法里面你try了但是你没catch呀
public static void func()
{
try
{
throw new Exception();
}catch(Exception e){}
finally
{
System.out.println("B");
}
}
加了后运行结果B A D
作者:
刘_鑫
时间:
2013-6-25 18:10
public static void func()throws Exception
{
try
{
throw new Exception();
}
finally
{
System.out.println("B");
}
}
声明抛出异常 结果 B C D
作者:
孙百鑫
时间:
2013-6-27 07:33
楼主您好。我已将您的帖子改成已解决。如果帖子发布长时间没加分。及时联系我。以免漏分的情况发生{:soso_e100:}
作者:
吴凯
时间:
2013-6-28 11:48
孙百鑫 发表于 2013-6-27 07:33
楼主您好。我已将您的帖子改成已解决。如果帖子发布长时间没加分。及时联系我。以免漏分的情况发生{:soso_e ...
啊.. 多谢版主啊 有时候忙 没上线, 多谢各位给我解答的同仁
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2