The course is part of this learning path
In this course, we'll learn about Exception Handling in Java.
- What is an Exception?
- Difference between Error and Exception
- Types of Exceptions
- Try-Catch Block
- Finally Block
- Throw and Throws Keywords
- Exception Methods
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there. In this video, we'll talk about the Throw Keyword. We use the throw keyword to explicitly throw an exception from the code. We can throw either checked or unchecked exceptions in this way. Throw keyword is used in the method. Let's look at a basic example of throwing an exception from the method. You see a divide method that returns the double value in the slide. In the method, because we can't divide a number by zero, we add throw ArithmeticException and pass a single string constructor parameter, which is an exception message, "Divider cannot be equal to zero". In our program, we handle this exception by using the try-catch block. Let's make an example with throw keyword.
In ExceptionHandling project, right click on the exception package and select 'New', 'Class'. Specify the class name as ThrowException, and select the checkbox for the main method, and click the 'Finish' button. Outside the main method, let's declare a static double method with two double parameters, public static double. The name of the method can be divide, and the parameters will be double x and double y. In this method, we'll check the value of variable y with the if statement. If the value of variable y is zero, we throw ArithmeticException by using the throw keyword. We pass the "Divider cannot be equal to zero" message to the constructor of ArithmeticException class. If the value of variable y is not zero, we return x / y. Let's call this method inside the main method. First, I'll declare a variable d with double type in the main method.
Now, I will call the divide method with parameters 8.5 and 0, and I'll assign this method to the variable d. Okay, let's run the code. As you can see, we got an exception named ArithmeticException. Divide method is invoked because the remainder is zero and ArithmeticException is thrown. The message that passed to the constructor is displayed in the output and program is terminated. We have to handle this exception using a try-catch block. In the main method, let's enclose the code that may throw an exception with the try-catch block. Inside the parentheses of the catch block, I will write the exception name, i.e., ArithmeticException. Also, we add object e. Now, I'll print the e object on the console.
In the print method, we use the exception object e to display the message in exception type. Okay, let's run the code. As you can see, the catch block handles the exception and exception type with "Divider cannot be equal to zero" message is displayed. Let's change the parameter 0 with 3.4. And let's print the value of variable d by using the print method. Okay, let's run the code. Result is 2.5. The catch block is not executed because there is no exception. Yes, I think the use of the throw keyword is understood. Let's take a short break here. See you in our next lesson.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.