«

»

Aug
04

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”);
}
}

VN:F [1.9.13_1145]
Rating: 7.7/10 (6 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 2 votes)
Java creating custom Exceptions, 7.7 out of 10 based on 6 ratings

About the author

Faisal Basra

Faisal Basra is an independent consultant, software developer, writer, blogger, speaker, architect and technology leader in Lahore, Pakistan. He has been a professional software developer since 2008, has been writing code since 2006. Having hands on experience of popular Java EE frameworks & technologies like JSF, Spring, Hibernate, Enverse, JPA, Richfaces, Primefaces, JSP/Servlet. I have taken many initiatives while working with teams. Some of includes Automated Build & Release Management system via Hudson, Maven, Archiva & SVN. Blogging is my hobby and I also initiated blog at corporate level from setting up complete blog for company, content generation strategy and visibility over the Internet by Internet Marketing. Framworks & Technologies: JSF, Richfaces, Primefaces, Openfaces, Struts, Hibernate, Spring, ORMLite Tools & Servers: jUnit, Log4j, Maven, Eclipse, MyEclipse, NetBeans, Tomcat, Jboss, WebLogic Mobile Development: Google Android Marketing: Internet Marketing, Mobile App Marketing

Permanent link to this article: http://www.javaplex.com/blog/java-creating-custom-exceptions/

1 comment

  1. Muhammad Salman says:

    its easy and good.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>