向上层抛出
函数可以不处理异常,交给上层函数处理
void test1() throws Exception
{
throw new Exception("test");
}
void test2() throws Exception
{
test1(); // 有异常抛出,但不在本函数中处理
函数可以不处理异常,交给上层函数处理
void test1() throws Exception
{
throw new Exception("test");
}
void test2() throws Exception
{
test1(); // 有异常抛出,但不在本函数中处理
}
代码如下:
HelloWorld.java
package my;
public class HelloWorld
{
public void test1()throws Exception
{
throw new Exception("test");
}
public void test2()throws Exception
{
test1();
}
public static void main(String[] args)
{
HelloWorld t=new HelloWorld();
try {
t.test2();
}catch(Exception e) {
e.printStackTrace();
}
}
}
运行结果如下: