Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

Step by step instructions to make your own particular Exception in Java

Author: First Enquiry
by First Enquiry
Posted: Apr 25, 2018

In the article Getting Started with Exception Handling in Java and we provide Best Java Institute In Bangalore, you know how to get toss and catch exemptions which are characterized by JDK, for example, IllegalArgumentException, IOException, NumberFormatException, and so on.

Imagine a scenario where you need to toss your own particular special cases. Envision you're composing an understudy administration program and you need to toss StudentException, StudentNotFoundException, StudentStoreException and so forth? So it's a great opportunity to make new special cases of your own.

We will call JDK's special cases worked in exemptions and call our own particular exemptions custom exemptions.

Wowser this: Writing custom special cases in Java is simple, yet the critical thing is, you ought to make this inquiry:

1. For what reason do I require custom exemptions?

For what reason do we have to make another special case, rather than utilizing the ones characterized by JDK?

The appropriate response could be exceptionally straightforward: When you couldn't locate any applicable special cases in the JDK, it's a great opportunity to make new ones of your own.

Be that as it may, how would we know which special case is significant, which isn't?

By taking a gander at the names of the exemptions to check whether its significance is suitable or not. For instance, the IllegalArgumentException is proper to toss while checking parameters of a technique; the IOException is suitable to toss when perusing/composing documents.

From my experience, the greater part of the cases we require custom special cases for speaking to business exemptions which are, at a level higher than specialized special cases characterized by JDK. For instance: InvalidAgeException, LowScoreException, TooManyStudentsException, and so forth.

2. Composing your own particular special case class

Presently, how about we perceive how to make a custom special case in real life. Here are the means:

  • Create another class whose name should end with Exception like ClassNameException. This is a tradition to separate an exemption class from consistent ones.
  • Make the class expands one of the exemptions which are subtypes of the java.lang.Exception class. By and large, a custom special case class dependably expands specifically from the Exception class.
  • Create a constructor with a String parameter which is the detail message of the special case. In this constructor, just call the super constructor and pass the message.

That is it. The accompanying is a custom special case class which is made by following the above advances:

public class StudentNotFoundException extends Exception {

public StudentNotFoundException(String message) {

super(message);

}

}

What's more, the accompanying case demonstrates the way a custom exemption is utilized is nothing not the same as implicit special case:

public class StudentManager {

public Student find(String studentID) throws StudentNotFoundException {

if (studentID.equals("123456")) {

return new Student();

} else {

throw new StudentNotFoundException(

"Could not find student with ID " + studentID);

}

}

}

Furthermore, the accompanying test program handles that exemption:

public class StudentTest {

public static void main(String[] args) {

StudentManager manager = new StudentManager();

try {

Student student = manager.find("0000001");

} catch (StudentNotFoundException ex) {

System.err.print(ex);

}

}

}

Run this program and you will see this yield:

1 StudentNotFoundException: Could not discover understudy with ID 0000001

3. Re-tossing a special case which is wrapped in a custom exemption

It's Java Training in Bangalore a typical practice for getting an implicit special case and re-tossing it by means of a custom exemption. To do as such, let add another constructor to our custom special case class. This constructor takes two parameters: the detail message and the reason for the special case. This constructor is executed in the Exception class as following:

public Exception(String message, Throwable cause)

Other than the detail message, this constructor takes a Throwable's subclass which is the inception (cause) of the present special case. For instance, make the StudentStoreException class as following:

public class StudentStoreException extends Exception {

public StudentStoreException(String message, Throwable cause) {

super(message, cause);

}

}

What's more, the accompanying case demonstrates where the StudentStoreException gets tossed:

public void save(Student student) throws StudentStoreException {

try {

// execute SQL statements..

} catch (SQLException ex) {

throw new StudentStoreException("Failed to save student", ex);

}

}

Here, assume that the spare() technique stores the predefined understudy data into a database utilizing JDBC. The code can toss SQLException. We get this exemption and toss another StudentStoreException which wraps the SQLExceptionas its motivation. Also, clearly the spare() strategy proclaims to toss StudentStoreException rather than SQLException.

So what is the advantage of re-tossing exemption like this?

For what reason not leave the first exemption to be tossed?

All things considered, the primary advantage of re-tossing special case by this way is including a higher deliberation layer for the exemption dealing with, which brings about more significant and clear API. Do you see StudentStoreException is more important than SQLException, isn't that right?

Nonetheless, make sure to incorporate the first special case with a specific end goal to save the reason so we won't lose the follow while troubleshooting the program when the exemption happened.

Furthermore, the accompanying code exhibits taking care of the StudentStoreException above:

StudentManager manager = new StudentManager();

try {

manager.save(new Student());

} catch (StudentStoreException ex) {

System.err.print(ex);

}

That is about the lesson of composing custom exemptions in Java.

About the Author

First enquiry is Best Training Institute in Bangalore. It act like bridge to students to learn courses in Bangalore.First enquiry institute will provide a free demo classes. Contact details: website: www.firstenquiry.com Phone Number: 8792462607

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: First Enquiry

First Enquiry

Member since: Apr 19, 2018
Published articles: 91

Related Articles