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

About 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 experience of building mission critical, large scale application in banking, finance & telecom field.

, ,

14 Responses to Java creating custom Exceptions

  1. Muhammad Salman February 16, 2010 at 10:31 am #

    its easy and good.

Trackbacks/Pingbacks

  1. jfd98ayhcim (http://jfdg98ayhcdi4gmf NULL.com/) - December 18, 2012

    Excellent website…

    the time to read or visit the content or sites we have linked to below the…

  2. lida (http://www NULL.lidahizlizayiflama NULL.com) - December 18, 2012

    Great Content…

    we like to honor many different internet pages on the web, even if they aren’t linked to us, by linking to them. Under are some webpages worth checking out…

  3. lida (http://www NULL.lidailezayiflama NULL.com) - December 18, 2012

    Sites we like…

    the time to read or visit the content or sites we have linked to below the…

  4. lida (http://www NULL.lida-slimming NULL.com) - December 19, 2012

    Blogs you should be reading…

    please visit the sites we follow, including this one, as it represents our picks from the web…

  5. lida (http://www NULL.lidazayiflama NULL.info) - December 19, 2012

    Websites you should visit…

    Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose…

  6. lida (http://www NULL.lidahapiankara NULL.com) - December 19, 2012

    Online Article…

    Every so often in a while we choose blogs that we read. Listed underneath are the latest sites that we choose…

  7. شات صوتي (http://chat-voice1 NULL.com/) - December 19, 2012

    Check this out…

    The information mentioned in the article are some of the best available…

  8. lida (http://www NULL.zayiflamailaci NULL.gen NULL.tr) - December 20, 2012

    Great website…

    please visit the sites we follow, including this one, as it represents our picks from the web…

  9. jfd98ayhcfim (http://jfdg98ayhcdi4gkgf NULL.com/) - December 20, 2012

    Cool sites…

    Here are some of the sites we recommend for our visitor…

  10. training management systems (http://www NULL.getadministrate NULL.com) - December 20, 2012

    Check this out…

    The information mentioned in the article are some of the best available…

  11. Business Growth (http://www NULL.DBestMerchants NULL.com) - December 21, 2012

    Check this out…

    The information mentioned in the article are some of the best available…

  12. Cash Advances (http://www NULL.EgelerMerchants NULL.com) - December 21, 2012

    Cool sites…

    Here are some of the sites we recommend for our visitor…

  13. Maximize your Profits (http://www NULL.BeaverCreekMerchants NULL.com) - December 21, 2012

    Websites we think you should visit…

    we like to honor many other internet sites on the web, even if they aren’t linked to us, by linking to them. Under are some webpages worth checking out…

Leave a Reply