vendredi 13 mars 2015

Coding Style for User defined Exception in java

I'm working on a java project. while browsing the existing code of User defined Exception class, i came across a the code written like below:



public final class ApplicationException extends RuntimeException {

public static void enforce(final String message) {
throw new ApplicationException(message);
}
private ApplicationException(final String message) {
super(message);
}
}


and whenever i have to throw an exception within code, I write



ApplicationException.enforce("exception message here");


I wanted to know is this some coding style or pattern i.e. enclosing the creation and throw of Exception in a public static method and calling that method whenever we want to throw exception?


What may be the reason developer chose this way to throw exception? why not directly write throw new ApplicationException("some message"); whenever required.


what is the gain using that style?


Thanks in advance


Aucun commentaire:

Enregistrer un commentaire