Question: I am moving from PHP to Java and I'm a little struggled.

I have this method like this that I use to get some data from MySQL database and I would like to treat the failure if no data got from database.

```
public double getRate() {
    double ret;
    try {
        // do a select query
        PreparedStatement stmt = conn.prepareStatement("SELECT `rate` FROM `rates` LIMIT 1");
        ResultSet rs = stmt.executeQuery();

        // get result
        rs.absolute(1);
        ret = rs.getDouble(1);

        // close the resources
        rs.close();
        stmt.close();
    }

    // this catches only the SQL errors (if I am right)
    catch (SQLException ex) {
        ex.printStackTrace();
    }

    // THIS IS WRONG BECAUSE "variable ret might not have been initialized"
    return ret;
}

```

In PHP we can return whatever in case of failure like this:

```
<?php

public function getRate() {
    $ret = db::getOne("SELECT `rate` FROM `rates` LIMIT 1");
    if ($ret) { 
        return $ret; // row has been found, so we return it
    } else {
        return false; // row hasn't been found, so we return false --> THIS is what I need to do in Java
    }
}

?>

```

**So how to treat a failure in Java methods/functions where I have nothing to return?**

Answer: 
You could make your method return an object of the double wrapper class `Double`. Then you could return a null pointer in case of some failure.

```
public Double getRate() {
    ...
    if(ok)
        return new Double(ret);
    else
        return null;
}

```
I am adding a sample code so you can understand how to handle such scenarios.
If your default value does not changes,it means there was nothing that matched your query.

```
public double methodName(int arg)
{
    double risk=0.0;
    String query = null;
    PreparedStatement stm = null;
    ResultSet r = null;

    Connection con=null;

    try{
        con=ConnectionDB.getConnection();
        if(con!=null)
        {
            query="select risk from table where year="+arg;
            stm = con.prepareStatement(query);
            r = stm.executeQuery();
            if(r.next())
            {
                risk=r.getDouble(1);
            }

        }

    }catch(Exception e)
    {
        e.printStackTrace();
    }
    finally{
        try {
            if (r != null) {
                r.close();
            }
            if (stm != null) {
                stm.close();
            }
            if(con!=null)
            {
                con.close();
            }

        } catch (Exception e) {
            System.out.println("" + e);
        }
    }
    return risk;

}

```