简体   繁体   English

为什么try catch中的return语句与'throws'一起工作

[英]why does a return statement inside a try catch work with 'throws'

does not work (Compilation error: missing return statement) 不起作用(编译错误:缺少return语句)

public SqlMapClientTemplate getSqlTempl() throws UivException, SQLException{
    try {
        SqlMapClient scl = (SqlMapClient) ApplicationInitializer.getApplicationContext().getBean("MySqlMapClient");
        DataSource dsc = (DataSource) ServiceLocator.getInstance().getDataSource(PIH_EIV_ORCL);
        return new SqlMapClientTemplate (dsc, scl);
    }
    catch (NamingException ne)
    {
        log.error(ne.getMessage(), ne);
    }
}

works: 作品:

public SqlMapClientTemplate getSqlTempl() throws UivException, SQLException{
    try {
        SqlMapClient scl = (SqlMapClient) ApplicationInitializer.getApplicationContext().getBean("MySqlMapClient");
        DataSource dsc = (DataSource) ServiceLocator.getInstance().getDataSource(PIH_EIV_ORCL);
        return new SqlMapClientTemplate (dsc, scl);
    }
    catch (NamingException ne)
    {
        log.error(ne.getMessage(), ne);
        throw new SQLException("Unable to get database connection: " + ne.getMessage());
    }
}

why? 为什么?

In first case the method is not returning anything after the catch block or inside the catch block. 在第一种情况下,该方法不会在catch块之后或catch块内返回任何内容。

In second case the catch block is throwing an exception so compiler know that the method will return an object or throw an exception. 在第二种情况下,catch块抛出异常,因此编译器知道该方法将返回一个对象或抛出异常。

In the first case if the exception is thrown there is no return value, the function just falls off the end, which is an error, same as: 在第一种情况下,如果抛出异常没有返回值,则函数刚刚结束,这是一个错误,同样如下:

public String foo() {
  int x = 5;
}

In the second the function is guaranteed to return a value or throw an exception. 在第二个函数中,函数保证返回一个值或抛出异常。

If you really just want to log the exception, but not take any other action like in the first example you could write something like: 如果你真的只想记录异常,但不采取像第一个例子中那样的任何其他动作,你可以编写如下内容:

public SqlMapClientTemplate getSqlTempl() throws UivException, SQLException{
    SqlMapClientTemplate ret = null; //set a default value in case of error
    try {
        SqlMapClient scl = (SqlMapClient) ApplicationInitializer.getApplicationContext().getBean("MySqlMapClient");
        DataSource dsc = (DataSource) ServiceLocator.getInstance().getDataSource(PIH_EIV_ORCL);
        ret = new SqlMapClientTemplate (dsc, scl);
    }
    catch (NamingException ne)
    {
        log.error(ne.getMessage(), ne);
    }
    return ret;
}

As Bhushan mentioned, the compiler can see in this instance that something will always happen there will be a return or an exception. 正如Bhushan所提到的,编译器可以在这个实例中看到一些东西总会发生,会有返回或异常。 In your first case if you get a Naming Exception you end up in an ambiguous state, nothing returns from a function that contractually has to have a return. 在你的第一种情况下,如果你得到一个命名异常,你最终会处于一个模糊的状态,没有任何东西从一个合同必须返回的函数返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM