Home / Programming / Java / Java creating custom Exceptions

Java creating custom Exceptions

Java supports the custom, user defined exception. Java provides the well known and common exception classes and gives freedom for developer to create their own exception classes matching to their specific business logic/role. For example, a user not found exception would be implemented by its own.

Download the Java Source code  for creating user custom exceptions

MyException.java  UserException.java

Step-1 Create the customized user exception, see the code below

public class UserException extends Exception{
String exception;
public UserException(){
public UserException(String exp){
public String getException(){
return this.exception;

Step-2 Use the user created exception in your own class, see the code below.

public class MyException {
private String user=””;
private String password=””;
* @param args
public static void main(String args[]){
MyException my=new MyException();
try {
my.validateUser(“”, “d”);
} catch (UserException e) {
// TODO Auto-generated catch block

public void validateUser(String user, String passwd)throws UserException{
if(this.user==user && this.password==passwd){
System.out.println(“User Validated Successfully”);
throw new UserException(“Invalid User Found”);

About Faisal Basra

Check Also

Securing Spring Remoting by Spring Security

If you are exposing your some of services by Spring Remoting’s Http Invoker, most probably ...