public class InputTest {
public static void main(String[] args)
{
C c = new C();
try
{
c.getExcC();
}catch(Exception e)
{
System.out.println(e.getClass().getName());
}
}
}
class Aexception extends Exception
{
}
class Bexception extends Exception
{
}
class C
{
public void getExcA() throws Aexception
{
throw new Aexception();
}
public void getExcB() throws Bexception
{
throw new Bexception();
}
public void getExcC() throws Exception
{
try
{
getExcA();
}finally
{
getExcB();
}
}
}
结果:Bexception
public static void main(String[] args)
{
C c = new C();
try
{
c.getExcC();
}catch(Exception e)
{
System.out.println(e.getClass().getName());
}
}
}
class Aexception extends Exception
{
}
class Bexception extends Exception
{
}
class C
{
public void getExcA() throws Aexception
{
throw new Aexception();
}
public void getExcB() throws Bexception
{
throw new Bexception();
}
public void getExcC() throws Exception
{
try
{
getExcA();
}finally
{
getExcB();
}
}
}
结果:Bexception
本文通过一个Java程序示例展示了如何在程序中使用异常处理机制。具体包括定义自定义异常类、抛出异常、以及如何在方法调用链中处理异常。特别关注了在不同方法间传递异常的过程,并演示了try-finally块的使用。
218

被折叠的 条评论
为什么被折叠?



