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: 
In your case I would prefer something like an `isHexDigit` method to using `NumberFormatException`, unless there are some assumptions that you can make about the format of your data - from your description it seems that there aren't such assumptions about when you'll encounter hex numbers vs. non-hex numbers.

This is because exceptions should be used to handle *exceptional conditions*, and if the expectation from your data is: *either hex digits or non-hex digits, separated by spaces*, then there's nothing exceptional about encountering a token that is other than a hex digit. 

Furthermore using the Exception *does* make the code less readable: without comments about the data, it hides the fact that interspersed non-hex digits are acceptable and expected input.

Having stated that preference, I might use exception handling to handle this case, and I certainly see plenty of code that does so. Lots of good functionality is wrapped up for you in that combination of decode/parseInt/NumberFormatException. I wouldn't use this without an explicit comment that clearly explains what I'm doing.
Its `Not about how good you code looks`, but `how good your code works`..........
`Ya offcourse it should be readable,` **As famously said...**

**Any fool can write a code that computer can understand, but only great programmers write codes that humans can understand.**

Its perfectly fine in some cases where, you need to have such kind of exceptions in place.

`When you want to catch multiple exceptions which belong to the same inheritance tree,` `then create a try block, and multiple catch blocks from more specific to more` `abstract.`

eg:

```
`Animal <--- Carnivores <--- Dog`

```

Now suppose there is a **DogException, CarnivoresException, AnimalException**.

Then it must be like this,

```
try{

           // your code 

     }
      catch(DogException d){}
      catch(CarnivoresException c){}
      catch( AnimalException a){}

```

**Above catches has been cascaded from more specific to more abstract so that the exception is caught to it very cause.**

**If there is no inheritance, then catch can be in any order...**