Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Custom Exception

  Asked By: Adelisa    Date: Oct 18    Category: Java    Views: 1146
  

Can anybody help in creating custom exceptions in
servlets and jsp..

I want create my own Exception Which can be put in a
try catch block and caught from servlets, jsp , java
beans etc..

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Benjamin Simpson     Answered On: Oct 18

ur project may have different layer. it is a good idea to have a layered exception  handling. define ur own exception class for every layer. they all should extend a parent exception which extends Exception.java.

in every layer u should catch  the lower layer exception and throw the exception of this layer.
u may have different types of exception constructors . this depends on what u want to do, what u want to throw and what u want to show.

as an example for service layer u can have this :


public class ServiceException extends ParentException
{
/**
* @deprecated dont use this
* @param errMsg
*/
public ServiceException(String errMsg)
{
this.setErrorMessage(errMsg);
}

/**
* @deprecated dont use this
* @param e
*/
public ServiceException(Throwable e)
{
this.setThrowable(e);
}

/**
* @deprecated dont use this
* @param errorNumber
* @param errMsg
* @param e
*/
public ServiceException(int errorNumber,String errMsg, Throwable e)
{
this.setErrorNumber(errorNumber);
this.setErrorMessage(errMsg);
this.setThrowable(e);
}

public ServiceException(int errorNumber,String errMsg)
{
this.setErrorNumber(errorNumber);
this.setErrorMessage(errMsg);
}

}



parent class is as follows:

public class ParentException extends java.lang.Exception
{
private String errorMessage = null;
private int errorNumber = 0;
private Throwable throwable = null;

public ParentException()
{
printStackTrace();
}

public void setErrorMessage(String errorMessage)
{
this.errorMessage = errorMessage;
}

public String getErrorMessage()
{
return errorMessage;
}

public void setErrorNumber(int errorNumber)
{
this.errorNumber = errorNumber;
}

public int getErrorNumber()
{
return errorNumber;
}

public ParentException(int errNumber,String errMsg,Throwable throwable)
{
this.errorMessage = errMsg;
this.errorNumber = errNumber;
this.throwable = throwable;
}
public ParentException(int errNumber,String errMsg)
{
this.errorMessage = errMsg;
this.errorNumber = errNumber;
}
public void setThrowable(Throwable throwable)
{
this.throwable = throwable;
}

public Throwable getThrowable()
{
return throwable;
}
}

a method in ur service layer can look like this :


public void myMethod(){
try{
//some operation
}
catch(the_lower_layer_exception_class x){
throw new ServiceException(x);
}
}

it is a long story how to show exception in a jsp. read some books for it. if u use web frameworks like struts, they have some easy ways to do that.

 
Didn't find what you were looking for? Find more on Custom Exception Or get search suggestion and latest updates.




Tagged: