2/27/08


Exception handling (continuation)

The great news for me that there are possibility to throw any type of exception in EJB's methods.
For example:

public class MyException extends Exception {
public MyException(String mess)

{ super(mess); }
}
So my EJB method might be:

@RemoteMethod public String someMethod(String param) throws MyException {
throw new MyException("Text");
}

Also client have to catch it. Pay attention that now stack trace will look like:

Exception in thread "main" com.epam.ejb.MyException: Test..

not the same as in case of throwing RuntimeException :

Exception in thread "main" java.rmi.RemoteException: EJB Exception: ; nested exception is:
java.lang.RuntimeException: Text..


Another interesting fact is understanding about exception chaining:

Exception e = new Exception();
if( e.getCause() instanceof MyException) { }

Exceptions handling


Sometimes unconscious using of a simple and usual things led to unexpected consequences. And than we look on those things with misunderstanding.
Let us consider so widely known conception as handling of exception in java programming language and its impact on the enterprise application's architecture.

There are some classes that represents exception:
javax.ejb.EJBException
java.rmi.RemoteException
java.lang.RuntimeException
java.lang.Exception
java.io.IOException
java.lang.ArithmeticException
java.lang.NullPointerException
java.sql.SQLExceptionThere are some questions:

What is the relation betwen these classes?
What kind of exception can be thrown in the EJB methods?
What kind of exception will be received by EJB or WebService client?
How EJB client can receive underlying exception infofmation?
How can client of the web service retrieve proper information about exception?
Is it necessity in delivering an underlying exceptions classes to the client application?
What is best practices to handle excepion in our code?
What is best place to implemests code to write logging messages?
How can I use java.lang.Error?

For answer on first quastion enough to look into java API documentation:

ArithmeticException extends RuntimeException
EJBException extends RuntimeException
NullPointerException extends RuntimeException
SQLException extends Exception
RuntimeException extends Exception
RemoteException extends IOException
IOException extends Exception
exception extends Throwable
Inside of implementation of EJB methods there is not facility to throw some checked exception, is there? Let's consider examples: if inside of such method present some like int i = 1/0; client will receive:

java.rmi.RemoteException: EJB Exception: ;
nested exception is: java.lang.ArithmeticException: / by zero at
weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:195)...


If you call throw new RuntimeException("Ops!"); you get in fist lines:

java.rmi.RemoteException: EJB Exception: ;
nested exception is: java.lang.RuntimeException: Ops! at
weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:195)...


If you write throw new java.rmi.RemoteException(); compiler will check:
"Unhandled exception type RemoteException".

What exactly is mean "EJB Exception" in end of first line of
stack trace?.. For sure we can throw EJBException inside above mentioned methods because it (EJBException) derivers its functionality from RuntimeException. What kind of exception we able to trows inside EJB methods except of RuntimeException? If exists ability to trows only RuntimeException what exactly does EJB container with these exceptions? Does it implement exception chaining?

Some useful links

http://mms.mts.com.ua/

I have started


Today I have found some interesting site: http://big-dot.blogspot.com. It was very unexpected for me, because.. the owner of that resource is my good friend Thor. Hello Thor I am glad to see you..
We were working in Ukrtelecom for one and half year. So I have already been working in EPAM about one year and he has been working in some another company in Kherson.
Our ways have parted but we have pretty good relation for now. And also we have quite similar looking for some things but not to all. We both have thechnology on a first place but not the same technology. He has chosen .NET instead ot the Delphi while I have chosen JAVA istead of .NET.
But my choice was not decision at all. It was just my little interest to new technology and some good luck. I sometimes argue with my friends about choice in like situation. For now I do not ready to explain my preferences between those ideology but I hope it will be not long time before I can. Not occasionally this post is first in the blog, I dedicate my blog for technology.. and what can be better to understanding some things than comparison of several ones?