在throw语句下面不能定义语句,代码1报错。但将throw语句封装成函数后,为什么下面可以定义语句?代码2编译通过。
代码1:编译失败。
class Test2
{
public static void func()
{
try
{
throw new Exception();
System.out.println("a");
}
catch (Exception e)
{
System.out.println("b");
}
}
public static void main(String[] args)
{
try
{
func();
}
catch (Exception e)
{
System.out.println("c");
}
System.out.println("d");
}
}
代码2:编译成功。
class Test2
{
public static void main(String[] args)
{
try
{
showExce();
System.out.println("a");
}
catch (Exception e)
{
System.out.println("b");
}
finally
{
System.out.println("c");
}
System.out.println("d");
}
public static void showExce()throws Exception
{
throw new Exception();
}
} |