Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
An exception is a problem that arises during the execution of a program. Java, being one of the most popular object-oriented language, provides a powerful mechanism to handle these exceptions. In this article, I will give you a brief into User-Defined Exceptions in Java.
Below topics are covered in this article:
Let’s get started!
An exception is a sudden and an unexpected event which leads to the sudden termination of the program during execution of a program that is at the runtime of a program. Below snapshot represents the execution flow of handling an exception.
We all someday or the other must-have encountered these exceptions which we had to fix in order to run the program. So we need to know about these types of exceptions first so that when occurred we can deal with these in no time.
Exceptions using Try-Catch block:
class MyException1 extends Exception{ String str1; MyException1(String str2) { str1=str2; } public String toString(){ return ("MyException Occurred: "+str1) ; } } class Example1{ public static void main(String args[]){ try{ System.out.println("Starting of try block"); throw new MyException1("This is error Message"); } catch(MyException1 exp){ System.out.println("Catch Block") ; System.out.println(exp) ; } } }
Here in the above snippet, I have demonstrated the use of try-catch block and how we can throw exceptions using that. The throw keyword is used to throw the exception by the user. IO Exception is used for this exception handling. The user-defined exception must contain a custom exception class. In the code, we have used a parameterized constructor which displays (This is error Message).
OUTPUT:
Starting of try block Catch Block MyException1 Occurred: This is error Message
Without using Try-Catch block
class InvalidProductException extends Exception { public InvalidProductException(String s) { // Call constructor of parent Exception super(s); } } public class Example1 { void Check(int weight) throws InvalidProductException{ if(weight<100){ throw new InvalidProductException("Product Invalid"); } } public static void main(String args[]) { Example1 obj = new Example1(); try { obj.productCheck(60); } catch (InvalidProductException ex) { System.out.println("Caught the exception"); System.out.println(ex.getMessage()); } } }
In this example, I am throwing the exception using the method. So here the throws keyword is used in the function signature or else it will show us an error.
Output
Caught the exception Product Invalid
Basically, that’s how it works. This brings us to the end of our blog on User Defined Exceptions in Java. I hope you found this blog informative and added value to your knowledge.
Check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “User Defined Exceptions in Java ” blog and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co