Come ti ha suggerito Ugly Mau se hai php5 puoi usare try catch, per info vedi il manuale.