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 这里声明了异常不处理怎么就行呢。

解决方案 »

  1.   

    不对吧,你这个程序没有出错处理语句,也对阿。
    不是没有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");
        }         }
    }
      

  2.   

    楼上可能没有看懂我的意思
    public class test7{
    public static void main(String[] args){
    method(); }
    static void method()  throws Exception 
    {
    try{
    System.out.println("test7");
    }
    finally
    {
    System.out.println("finally");
    }
    }}
    这么写就错了。因为static void method()  throws Exception 这里声明了异常不处理不行
    public class test7{
    public static void main(String[] args)throws Exception {
    method(); }
    static void method()  
    {
    try{
    System.out.println("test7");
    }
    finally
    {
    System.out.println("finally");
    }
    }}
    这么写就可以,public static void main(String[] args)throws Exception 这里声明了异常不处理怎么就行呢
      

  3.   

    public static void main(String[] args)throws Exception 这里声明了异常
    是把异常抛给虚拟机了
      

  4.   

    try不一定要带catch,带个finally一样可以
      

  5.   

    既然throws了就是把异常处理交给外面处理,外面的方法当然要处理他,或者再扔给外面处理
    既然method把可能的异常throws了,那么调用它的main就要处理它,如果main不处理,那么main就再throws
      

  6.   

    method()方法抛出了异常 外面调用它的main()方法可以用try{}catch()捕获异常,也可以throws Exception继续抛出异常。