Thank you so much.
Here is my ServiceException, the base exception class for Service Layer
Code:
package com.XXX.service.exception;
import com.XXX.exception.BaseException;
/**
* Base checked exception for the Middle Tier.
*
*
*/
public class ServiceException extends BaseException{
/**
* Constructor
*/
public ServiceException() {
super();
}
/**
* Constructor with error message.
*
* @param msg the error message associated with the exception
*/
public ServiceException(String msg) {
super(msg);
}
/**
* Constructor with root cause.
*
* @param cause the root cause of the exception
*/
public ServiceException(Throwable cause) {
super(cause);
}
/**
* Constructor with error message and root cause.
*
* @param msg the error message associated with the exception
* @param cause the root cause of the exception
*/
public ServiceException(String msg, Throwable cause) {
super(msg, cause);
}
}
package com.XXX.exception;
public class BaseException extends RuntimeException {
public BaseException() {
super();
}
/**
* Constructor with error message.
*
* @param msg the error message associated with the exception
*/
public BaseException(String message) {
super(message);
}
/**
* Constructor with root cause.
*
* @param cause the root cause of the exception
*/
public BaseException(Throwable cause) {
super(cause);
}
/**
* Constructor with error message and root cause.
*
* @param msg the error message associated with the exception
* @param cause the root cause of the exception
*/
public BaseException(String message, Throwable cause) {
super(message, cause);
}
}
In my earlier posting, the code snippet in Spring bean try/catch block looks for Exception Message for example specific exception like "DetachEntityException" and if it occurs its handled in the Spring/JSF layer, otherwise it throws ServiceException as shown below
Code:
try {
deviceTypeDao.persist(deviceType);
//deviceTypeDao.saveOrUpdate(deviceType);
//deviceTypeDao.remove(deviceType);
} catch(Exception ex){
System.out.println("catching Exception In Spring Bean");
Throwable lastCause = ex;
while (lastCause.getCause() != null){
lastCause = lastCause.getCause();
System.out.println("Exception Cause="+lastCause);
}
System.out.println("Exception Message="+lastCause.getMessage());
if (lastCause.getMessage().contains("detached entity passed to persist"))
throw new DetachEntityException("detached entity passed to persist.");
else
throw new ServiceException(); //BaseException class
}
}
The reason i say the above code doesnt do anything is maybe i am missing something like i have to write a test-code which generate Exception and eventually Rollback the transaction. Any pointers/suggestions will be highly appreciated