Wednesday, December 7, 2011

Unresolved compilation problem

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Unhandled exception type Exception

A java file cannot be ran unless and until it is compilation error free. JVM compiler understands any syntax error or compile time error before it could create runable byte code (.class file). Hence JVM can run a java program only after successful compilation. Look for the compile time error if you get above error.

JVM will run the code no matter if there is any run time exception. If there is any run time exception that will handled at run time accordingly. You must ensure that atleast a code do not contains any syntax error and is compileable.

How to solve compile error?

Solving compilation error is very simple, all you need to do is follow all rules and regulation of JVM. To solve compilation error you need to check for Syntax error, Scope of a variable, Non reachable statements, Exceptions handling etc

Solving compilation error is simple because it can be viewed. A simple Google search can solve your compilation error. Also IDEs like Eclipse, NetBeans etc has advance features which can show any compile error while wring the code itself i.e. before even compilation.

How to solve run time error/exceptions?

My personal experience says solving a RUN time error could me very tough in some cases. You may even require to spend overnight to solve a simple run time exceptions because it cannot be viewed by compiler. In most cases there is a data issue. Run time exception could be simply a null pointer exception which occurs because the object is null.

Example :

Code
System.out.println(tempStr.trim());
Can throw
Exception in thread "main" java.lang.NullPointerException
because tempStr is null.

The answer to the above question is debugging i.e. try to debug your code. Put logs wherever you doubt for the values. In the above example if you print the value of tempStr you will recognize the root cause of the exception. Also try to put exception handling codes i.e. try { .. } catch (Exception ex) {..} wherever you doubt for the values.