class Demo
{
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();
}
}
/*上面这个程序因为将抛出异常封装成一个对象,所以System.out.println("A");不受影响。
因为封装成对象后这个showExce();方法是可能发生也可能不发生异常.*/
class Demo showExce();
{
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("D");
}
}
}
/*这个程序编译失败!!!
这个程序中是直接将抛出异常定义在了打印语句上面,打印语句是不能执行的,即定义了一句废话!!
不把异常封装成对象那么这个异常肯定发生!!!*/
当异常封装对象后再使用和不封装对象使用的区别
最新推荐文章于 2023-04-17 05:15:00 发布