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(){
super();
exception="Unknown";
}
public UserException(String exp){
super(exp);
this.exception=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
e.printStackTrace();
}
}
public void validateUser(String user, String passwd)throws UserException{
if(this.user==user && this.password==passwd){
System.out.println(”User Validated Successfully”);
}
else
throw new UserException(”Invalid User Found”);
}
}
Posted in Java














I, Faisal Basra, keen of Java development. My area of interest are Enterprise java development using latest frameworks like Hiberantes, Struts, JSF and Spring. Currently, I am working in NetSol working in Java EE field.
Apart from the java development, I also have interest in Internet marketing specially in the field of Search Engine Marketing / Search Engine Optimization.
The, very next interest I have is of Web Development side. The Internet speaks the language of PHP, MySQL, Apache and Linux thus mostly said simply its LAMP. So, I also have developed several websites using PHP and related technologies.
