java中throws子句是怎么用的?工作原理是什么?

throws子句 
马克-to-win:当你的方法里抛出了checked异常,如你不catch,代表你当时不处理(不想处理或没条件处理),但你必须得通过"throws 那个异常"告诉系统说,这儿有个问题,我现在不处理,将来一定别人要处理,否则执行到它,马克-to-win:系统会"不优雅"的崩溃。举个例子,工兵张三发现了地雷,假如他处理,完事就完事儿了。但是他发现了地雷,自己却没带齐工具,没法处理,他必须做个标记,说这儿有一个地雷,别的工兵将来一定要处理,否则将来有人踩上去会爆炸。马克-to-win:注意:throws只是标记,并没处理,执行到那,系统还是会崩溃!

马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。



马克-to-win:语法总结就是:当你的方法里抛出了checked异常,如你不catch,必须throws,即告诉编译器,我的调用者会处理。如果你已经是main,则main的调用者jvm会替你收拾残局。否则无法编译通过。

马克-to-win:有的同学可能会问:throws有什么意义?又不真正处理问题。throws的意义,在于和throw配合起来一起工作。有关throw的意义,请参照上面throw部分。

马克-to-win:现在就出现了一个非常深入的问题。(新手可忽略)为什么sun公司的语法设计成:runtime异常不需要throws,而非 runtime异常需要呢?咱们先说非runtime异常为什么需要throws呢?因为程序员多一道工序宣称一下,麻烦一下自己,会给sun公司的人(Java编译器)提供很大便利,少了很多判断等工作。说穿了就是麻烦我们自己方便他人。而为什么runtime异常不需要throws呢?因为运行时的情况非常复杂,程序员实在无法预料到所有的情况到底除零还是格式不对,所以即使想帮也帮不上忙。sun公司的人没有办法,只能自己去判断,即使再不愿意,也得干。于是也就不用你throws了。下面一段话摘自国外程序员和sun公司的对话:有关抱怨sun公司强迫他们写throws的,认为是个 burden。挺有意思,大家可以看看。事儿(需要throws)都是programmer干的,出了问题,责任也是你的。


马克-to-win:拿上一节,1.6.2为例,当时有问题,我们用catch解决了,当然我们也可以用throws技术搞定它。




例:1.7.1(本例编译有错误)-本章源码

import java.io.FileNotFoundException;
public class Test {
    void m1_mark_to_win()  {
        throw new FileNotFoundException();
    }
}

马克-to-win:上例编译有错误,因为FileNotFoundException是checked异常,所以我们必须加catch马上处理或用throws留待将来处理。catch我们这里就不说了。马克-to-win:上节有论述。我们这里用throws 来处理。


例:1.7.2(可通过编译)-本章源码

import java.io.FileNotFoundException;
public class Test {
    void m1_mark_to_win() throws FileNotFoundException  {
        throw new FileNotFoundException();
    }
}

像下面的子类也可以




例:1.7.2_2(可通过编译)-本章源码
import java.io.*;
public class Test {
    void m1_mark_to_win() throws IOException  {
        throw new FileNotFoundException();
    }
}

这里FileNotFoundException是IOException 的子类。

java.lang.Object
  java.lang.Throwable
   java.lang.Exception
    java.io.IOException
      java.io.FileNotFoundException


马克-to-win:注意:例:1.7.2,编译就没问题了,但是throws FileNotFoundException,只是标记了一下问题,并没有真正处理,运行到throw new FileNotFoundException();时,程序还是会崩溃。马克-to-win:不信,下面我们就加一个主调方法测试一下。

例:1.7.3(本例编译有问题)-本章源码

import java.io.FileNotFoundException;
public class Test {
    void m1_mark_to_win() throws FileNotFoundException  {
        throw new FileNotFoundException();
    }
    public static void main(String[] args)   {
        Test t=new Test();
        t.m1_mark_to_win();//这句会报红线
        System.out.println("马克-to-win:优雅结束");
    }
}

马克-to-win:例:1.7.3的主调方法t.m1_mark_to_win();又有问题了,因为被调方法m1_mark_to_win()里面有一个没有处理的throw异常,所以主调方法还是编译不过。马克-to-win:我们必须或者catch或者throws处理,catch上一节讲了,现在我们不讲。这里我们用throws,等于还是没有处理,程序运行到这还是会崩溃。马克-to-win:下面我们看一下运行结果。




例:1.7.4-本章源码

import java.io.FileNotFoundException;
public class Test {
    void m1_mark_to_win() throws FileNotFoundException  {
        throw new FileNotFoundException();
    }
    public static void main(String[] args) throws FileNotFoundException   {
        Test t=new Test();
        t.m1_mark_to_win();
        System.out.println("马克-to-win:优雅结束");
    }
}

输出结果:

Exception in thread "main" java.io.FileNotFoundException
    at Test.m1_mark_to_win(Test.java:4)
    at Test.main(Test.java:8)

马克-to-win:结果分析:当程序运行t.m1_mark_to_win();程序崩溃了,连System.out.println("马克-to-win:优雅结束");都没有执行。要想不崩溃, 只能加try catch。

例:1.7.5-本章源码

import java.io.FileNotFoundException;
public class Test {
    void m1_mark_to_win() throws FileNotFoundException  {
        throw new FileNotFoundException();
    }
    public static void main(String[] args)   {
        Test t=new Test();
        try {
            t.m1_mark_to_win();
        } catch (FileNotFoundException e) {
            System.out.println(e);
        }
      System.out.println("马克-to-win:优雅结束");
    }
}

输出结果:

java.io.FileNotFoundException
马克-to-win:优雅结束

马克-to-win:下面的例子1.7.6,多了一层调用,道理是一样的, 仅供参考。一直用throws做缓兵之计,没有解决问题,所以程序最后还是崩溃了!catch才能真正解决问题。




例:1.7.6-本章源码
import java.io.FileNotFoundException;
class Class_mark_to_win
{
    void m1_mark_to_win() throws FileNotFoundException {
        throw new FileNotFoundException();
    }
}
class Class2_mark_to_win
{
    void m2_mark_to_win() throws FileNotFoundException {
        Class_mark_to_win c=new Class_mark_to_win();
        c.m1_mark_to_win();
    }
}
public class Test {
    public static void main(String[] args) throws FileNotFoundException    {
        Class2_mark_to_win c2=new Class2_mark_to_win();
        c2.m2_mark_to_win();
        System.out.println("马克-to-win:优雅结束");
    }
}

输出结果:
Exception in thread "main" java.io.FileNotFoundException
    at Class_mark_to_win.m1_mark_to_win(Test.java:5)
    at Class2_mark_to_win.m2_mark_to_win(Test.java:12)
    at Test.main(Test.java:20)