Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

spring

does not work (Compilation error: missing return statement)

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?

like image 938
Omnipresent Avatar asked Nov 06 '09 02:11

Omnipresent


People also ask

Should return be inside try catch?

Putting return where it makes sense improves readability and maintainability and makes your code simpler to understand. You shouldn't care as finally block will get executed if a return statement is encountered. Save this answer. Show activity on this post.

What happens if we place return statement in try catch block?

In a try-catch-finally block that has return statements, only the value from the finally block will be returned.

Where do return statements go on try catch?

1 Write return statement inside catch-block & at the end of method; that is just before end of method.

Can you return in a Catch statement?

You can return normally from catch block. It's normally good functional code. Save this answer.


3 Answers

In first case the method is not returning anything after the catch block or inside the catch block.

In second case the catch block is throwing an exception so compiler know that the method will return an object or throw an exception.

like image 79
Bhushan Bhangale Avatar answered Nov 10 '22 00:11

Bhushan Bhangale


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;
}
like image 21
zimbu668 Avatar answered Nov 09 '22 22:11

zimbu668


As Bhushan mentioned, the compiler can see in this instance that something will always happen there will be a return or an exception. 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.

like image 32
Matt Avatar answered Nov 09 '22 23:11

Matt