Skip to content

Instantly share code, notes, and snippets.

@kenorb
Last active August 29, 2015 14:04
Show Gist options
  • Save kenorb/cb67ac6e54cf509d80a4 to your computer and use it in GitHub Desktop.
Save kenorb/cb67ac6e54cf509d80a4 to your computer and use it in GitHub Desktop.
User Defined Exception Demo
/*
* User Defined Exception Demo
*
* 1. Create a class by extending Exception class
* 2. Write a parameterized constructor.
* 3. Override the toString() method.
*
* Usage:
* javac UserExceptionDemo.java
* java UserExceptionDemo 2 10
* java UserExceptionDemo 10 2
*/
class MyException extends Exception {
String message;
MyException(String message) { // Parameterized constructor.
this.message = message;
}
public String toString() { // Returns a string representation of the object.
return this.message;
}
}
class UserExceptionDemo {
public static void main(String args[]) {
try {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
if (a>b) {
throw new MyException("a is max"); // Invoke user defined exception.
} else {
throw new MyException("b is max or equal"); // Invoke user defined exception.
}
} // end of try
catch (MyException m) {
System.out.println(m); // Invokes toString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment