Create and How to use your own Exceptions

Creating Your Own Exception Class

● Steps to follow
– Create a class that extends the RuntimeException or the
Exception class
– Customize the class
● Members and constructors may be added to the class
● Example:
 class HateStringExp extends RuntimeException {
 /* some code */

How To Use Your Own Exceptions
class TestHateString {
 public static void main(String args[]) {
 String input = "invalid input";
 try {
 if (input.equals("invalid input")) {
 throw new HateStringExp();
System.out.println("Accept string.");
 } catch (HateStringExp e) {
 System.out.println("Hate string!”);
 } }

People who read this post also read :


Post a Comment


Twitter Delicious Facebook Digg Stumbleupon Favorites More