Throw Keyword
Start course

In this course, we will learn the concepts of microservice and spring framework and focus on Exception Handling.

Learning Objectives

  • Exception Handling in Java 

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud


  • Basic Java knowledge

Hello there. In this video, we will 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 exception message, "Divider cannot be equal to zero". In our program, we handle this exception by using the try catch block. Now let's try to understand the throw keyword through an example. In exception handling 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 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 will check the value of variable y with the if statement. If the value of variable y is zero, we throw an arithmetic exception by using the throw keyword. We passed 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 will declare a variable d of double type in the main method. Now I will call the divide method with parameters 8.5 and 0, and I will assign this method to 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 arithmetic exception is thrown. The message that was passed to the constructor is displayed in the output and the 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 a try catch block. I will write the exception name inside the parenthesis of the catch block, i.e. ArithmeticException. Also we add object e. Now, I will print the e object on the console. In the print method, we use the exception object e to display the message and exception type. Okay, let's run the code. As you can see, the catch block handles the exception and displays the message, 'Divider cannot be equal to zero'. 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. Okay, I think the use of the throw keyword is understood. Let's take a short break here. See you in our next lesson, friend.


About the Author
Learning Paths

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.