Java provides us facility to create our own exceptions which are basically derived classes of Exception. For example MyException in below code extends the Exception class.
We pass the string to the constructor of the super class- Exception which is obtained using “getMessage()” function on the object created.
In the above code, constructor of MyException requires a string as its argument. The string is passed to parent class Exception’s constructor using super(). The constructor of Exception class can also be called without a parameter and call to super is not mandatory.
This article is contributed by Pranjal Mathur. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Exception Propagation in Java
- Types of Exception in Java with Examples
- Understanding OutOfMemoryError Exception in Java
- Null Pointer Exception In Java
- Comparison of Exception Handling in C++ and Java
- Infinity or Exception in Java when divide by 0?
- Output of Java program | Set 12(Exception Handling)
- Understanding Array IndexOutofbounds Exception in Java
- 3 Different ways to print Exception messages in Java
- Exception Handling with Method Overriding in Java
- Nested try blocks in Exception Handling in Java
- Custom ArrayList in Java
- Version Enhancements in Exception Handling introduced in Java SE 7
- Java Swing | Creating Custom Message Dialogs
- Exception handling in JSP
- Exception Handling in C++
- Python Exception Handling
- How to add Custom Spinner in android?
- How to add a custom styled Toast in Android
- How to create a custom AlertDialog in Android