Suresh Rohan's Blog

This blog is all about Java, J2EE,Spring, Angular, React JS, NoSQL, Microservices, DevOps, BigData, Tutorials, Tips, Best practice, Interview questions, Views, News, Articles, Techniques, Code Samples, Reference Application and much more

Monday, November 2, 2015

Custom Exceptions in Java

There are two options: you can extend either the Exception or RuntimeException class depending on your need. 

If you want to force the users of your custom exception to handle the exception, then you can extend your exception class from the Exception class, which will make your custom exception a checked exception. 

If you want to give flexibility to the users of your custom exception, and leave it to the users of your exception to decide if they want to handle the exception or not, you can derive your exception from the RuntimeException class.

How about extending the Throwable or Error class for custom exceptions? 
  • The Throwable class is too generic to make it the base class of your exception, so it is not recommended. 
  • The Error class is reserved for fatal exceptions that the JVM can throw (such as StackOverflowError), so it is not advisable to make this the base class of your exception.


Example

BusinessException.java

package com.appfworks.exception;

import java.io.IOException;
import java.io.Serializable;
import java.util.Locale;

/**
* Created by suresh on 02/11/15.
*/
public class BusinessException extends Exception implements Serializable {
private static final long serialVersionUID = 1L;
protected String errorCode;
protected String errorDesc;
protected String description;
protected String lineAtEx;

protected BusinessException() { }
public static BusinessException getInstance(String errorCode) {
return new BusinessException(errorCode);
}

public static BusinessException getInstance(String errorCode, Throwable ex) {
return new BusinessException(errorCode, ex);
}

public static BusinessException getInstance(String errorCode, String description) {
BusinessException exception = new BusinessException(errorCode);
exception.description = (description==null)?"Unknown description" : description;
return exception;
}

public static BusinessException getInstance(String errorCode, String description, String... messages) {
BusinessException exception = new BusinessException(errorCode);
exception.description = (description==null)?"Unknown description" : description;
if (messages!=null) {
for (String m:messages) {
exception.errorDesc = exception.errorDesc.replaceFirst("%",m);
}
}
return exception;
}

public static BusinessException getInstance(String errorCode, String description, Throwable ex) {
final BusinessException exception = new BusinessException(errorCode, ex);
exception.description = StringUtils.defaultString(description, "Unknown description");
return exception;
}

public static BusinessException getInstance(String errorCode, Locale l) {
return new BusinessException(errorCode, l);
}

public static BusinessException getInstance(String errorCode, Throwable ex, Locale l) {
return new BusinessException(errorCode, ex, l);
}

public static BusinessException getInstance(String errorCode, String description, Locale l) {
return getInstance(errorCode, description, null, l);
}

public static BusinessException getInstance(String errorCode, String description, Throwable ex, Locale l) {
BusinessException exception = new BusinessException(errorCode, l);
exception.description = (description==null)?"Unknown description" : description;
return exception;
}

public String getErrorCode() {
return errorCode;
}

public String getErrorDesc() {
return errorDesc;
}

public String getDescription() {
return (Validator.isBlank(description) ? "" : description);
}

public String toString() {
StringBuffer sb = new StringBuffer(errorCode );

sb.append(":").append(errorDesc);

if (!Validator.isBlank(description))
sb.append(":").append(description);
if (!Validator.isBlank(lineAtEx))
sb.append(":").append(lineAtEx);
return sb.toString();
}
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
this.serialize(out);
}

protected void serialize(java.io.ObjectOutputStream out) throws IOException {
out.write((int)serialVersionUID);
out.writeUTF(errorCode);
out.writeUTF(errorDesc);
out.writeUTF(description);
out.writeUTF(lineAtEx);
}

// it is clear that readObject/deSerailize [sic] were never called:
private void readObject(java.io.ObjectInputStream in)throws IOException,
ClassNotFoundException {
this.deSerialize(in);
}

protected void deSerialize(java.io.ObjectInputStream in)throws IOException,
ClassNotFoundException {
int s = in.read();
if(s>=1){
errorCode = in.readUTF();
errorDesc = in.readUTF();
description = in.readUTF();
lineAtEx = in.readUTF();
}
}

@Override
public String getMessage(){
return this.errorDesc;
}


protected BusinessException(String errorCode, Locale l){
this.errorCode = errorCode;
this.description = ":";
localizeErrorDesc(l);
}

protected BusinessException(String errorCode, String description, Locale l){
this.errorCode = errorCode;
this.description = (description==null)?"Unknown description" : description;
localizeErrorDesc(l);
}

protected BusinessException(String errorCode, Throwable ex, Locale l){
super(errorCode, ex);
this.errorCode = errorCode;
localizeErrorDesc(l);
this.description = ":";
}


public String getLocaleErrorDesc(String errorCode2, Locale locale) {
String errorDesc = getLocaleErrorDescription(errorCode, locale);
if(errorDesc != null){
this.errorDesc = errorDesc;
}else{
this.errorDesc= getErrorDescription(errorCode).getDescription();
}
return this.errorDesc;
}
}



No comments:

Post a Comment