Question: I recently switched from MySQL to PostgreSQL. I have one problem left however.

Previously, I would store small images in the BLOB format in MySQL.

PostgreSQL doesn't know such thing as a BLOB.

I tried using BYTEA field type instead. This actually inserts an large (hexadecimal?) string I guess, but now I'm stuck trying to get this string back to displaying an actual image in PHP..

Any ideas? Thanks in advance.

Here is a piece of code I use to save the image in the database:

```
$data = bin2hex(file_get_contents('php://input'));

if (!empty($data)) {
  $sql = "UPDATE asset SET data = X'%s' WHERE uuid = '%s'";
  $args = array($data, $asset_uuid);
}

```

psql (9.1.3) and php 5.3.6 are used

Answer: 
Exception handling is an integral part (and one of the design goals) of the Java programming language... you shouldn't cast them off just because you think they are "ugly".

That said, if you want a simple and readable way to handle `NumberFormatException`s, you might consider using the [`NumberUtils`](http://tinyurl.com/6qalmct) class instead. 

The `toInt(String str, int defaultValue)` method converts a `String` to an `int`, returning a default value if the conversion fails. If the string is `null`, the default value is returned.

```
 NumberUtils.toInt(null, 1) = 1
 NumberUtils.toInt("", 1)   = 1
 NumberUtils.toInt("1", 0)  = 1

```

The method encapsulates exception catching and handling, as seen in the [source code](http://tinyurl.com/6rt8cmm) below. As a result, the client only needs to make a single method call.

```
public static int toInt(String str, int defaultValue) {         
    if(str == null) {
        return defaultValue;
    }
    try {
        return Integer.parseInt(str);
    } catch (NumberFormatException nfe) {
        return defaultValue;
    }
}

```
It's the best you can do. A method is either going to return some kind of success/error indicator or its going to throw an exception, it's just a question of which is most convenient. Here Sun's made the decision for us, so no need for a debate.

What does rather bug me about this is that the exception is going to include a full stack trace! In your particular case, if you are reading millions of these strings, you *will* notice the (completely unnecessary) poor performance. If it matters to you, you might want to consider writing your own method (you can use the Sun code as a guide.) Then you can decide for yourself whether you want to use an exception or not. If you do, keep a static copy of the exception handy and always throw that to save allocation time. And override `fillInStackTrace` so it does nothing and you don't have a meaningless stack trace in your exception.