venerdì 10 settembre 2010

Errors and Exceptions....

Seems a simple argument, but often junior programmers ignore that there are Errors!
Exception handling mechanism is well known, but the fact that Exceptions are one of the two separate sub-groups of the Throwable class is not often mastered.
The result is that often we catch exceptions but ignore errors.
Is just a little thing we have to keep in mind, mainly if your programs execute inside complex environmets.
Infact an unmanaged error could cause the whole JVM to close while you think that you have managed all the possible exceptions.

Yes: you managed all possible exceptions, but not a single error ! ;)

But there is a good news for all of you folks : Errors are exactly as Exceptions, so you can manage them the same way !

So why do we have two different groups ?
Becouse Exceptions are things you should not make to happen but must manage inside of you, while Errors are lightings hitting you when you can't do much to protect yourself.... as OutOfMemoryError !

But no metter the difference between the two group, let's manage even Errors, it's not so difficult after all:

catch(Exception ex)
catch (Error er)

Have a lot of fun with your JavaCode!

1 commento:

  1. By the way:
    a couple of day after I wrote this first post, a collegue got a serious problem with a JBoss web application.....
    He was not able to manage a wrong situation, even if he put two catch: one for a specific webServices exception and an absolute generic exception.....
    But the problem was:
    ClassNotFoundError due to a run time compilation of some stubs

    All's well what ends well, but next time don't loose hours looking for Exceptions when you get Errors !