日期:2014-05-20  浏览次数:20824 次

throws问题
public   class   test7{


public   static   void   main(String[]   args)throws   Exception   {
method();

}
static   void   method()     throws   Exception  
{
try{
System.out.println( "test7 ");
}
finally
{
System.out.println( "finally ");
}
}

}
代码如上,不明白在static   void   method()     throws   Exception   这里声明了异常不处理不行,但是在public   static   void   main(String[]   args)throws   Exception   这里声明了异常不处理怎么就行呢。

------解决方案--------------------
不对吧,你这个程序没有出错处理语句,也对阿。
不是没有catch语句吗?也能执行阿,没错呢!
你是不是说不能没有final语句阿,但如果你加上了catch语句就可以去掉final语句了。
public class Test7{


public static void main(String[] args) throws Exception {
method();

}
static void method() throws Exception
{
try{
System.out.println( "test7 ");
}
catch(Exception e){
System.err.print( "Finally ");
}

}
}
------解决方案--------------------
public static void main(String[] args)throws Exception 这里声明了异常
是把异常抛给虚拟机了
------解决方案--------------------
try不一定要带catch,带个finally一样可以
------解决方案--------------------
既然throws了就是把异常处理交给外面处理,外面的方法当然要处理他,或者再扔给外面处理
既然method把可能的异常throws了,那么调用它的main就要处理它,如果main不处理,那么main就再throws。。。
------解决方案--------------------
method()方法抛出了异常 外面调用它的main()方法可以用try{}catch()捕获异常,也可以throws Exception继续抛出异常。
------解决方案--------------------
是啊!继续抛出异常后就叫给虚拟机处理了