方法抛出的未处理异常不会阻止编译

时间:2017-03-15 00:23:56

标签: java methods exception-handling

所以我在一个类中有这两个方法:

public String getConsumerKey() throws FileNotFoundException, IOException
{
    String consumerKey;
    consumerKey = getPropertyValueOrNull(getConfigPath(CONSUMERVALUESCONFIGNAME),"consumer_key");
    return consumerKey;     
}

private String getPropertyValueOrNull(String path, String key) throws FileNotFoundException, IOException
{   
    Properties prop = new Properties();
    String value = null;

    // load a properties file
    prop.load(new FileInputStream(path));
    value = prop.getProperty(key);

    if ( value == null || value.isEmpty())
    {
        value = null;
    }
    return value;
}

我从主方法中调用getCosumerKey(),如下所示:

public static void main(String[] args) 
{
        try {
            MyClass.getInstance().getConsumerKey();
        } catch (IOException e) {
            e.printStackTrace();
        }

}

当我运行此代码时,除了我收到FileNotFoundExcpetion之外没有任何问题。当我尝试添加一个catch块来处理FileNotFoundException时,我收到一条错误,说已经从IOException catch块处理了异常。

编译器是不是应该阻止我运行代码,为什么编译器不允许我处理异常?

0 个答案:

没有答案
相关问题