The course is part of this learning path
In this course, we will learn the concepts of microservice and spring framework and focus on Exception Handling.
- Exception Handling in Java
- 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 throws keyword. We use the throws keyword in the method declaration. If we know which exceptions can be thrown from this method, we use the throws keyword. In the method definition, if you're using a risky piece of code without implementing the try-catch block, you can declare it throws an exception. The collar method might handle these exceptions with the try-catch block. If we call this method from the main class, we handle these exceptions there. In our example, we have a void divide method that has no parameters. We throw two exception classes: the first one is NumberFormatException, and the second one is ArithmeticException. The first parseInt method throws NumberFormatException because 8b is not a number format. In division operation, if the first number is a number format and the divider is equal to zero, this time it throws ArithmeticException.
To better understand the use of the throws keyword, let's move on to the Eclipse and make an example. In exception handling project, right-click on the 'Exception Package' and select new class. Specify the class name as ThrowsException and select the checkbox for the main method and click the 'Finish' button. Outside the main method, let's declare a static void method without parameters. public static void; the name of the method can be divide. Also, this method will throw NumberFormatException and ArithmeticException. So, after the name of the method, I write NumberFormatException. And after the comma, I write ArithmeticException. In the divide method, we declare the variable int a and assign the parseInt method of the integer class with the value 8b to it. We declare the second variable int b and assign the parseInt method of the integer class with value zero to it.
The parseInt method is used for parsing a string argument into an integer object. I will declare the last variable int c and assign a / b to it. And I will display the value of variable c by using the print method. System.out.println("Results: " + c); and I will call the divide method inside the main method. We need to handle these two exceptions that the divide method throws. Let's enclose the code that may throw an exception with a try-catch block. We use the try keyword above the divide method in the main method. In the first catch block, we handle NumberFormatException. In this catch block, we display the invalid number format message by using the print method. In the second catch block, we handle ArithmeticException. By using the print method, we display the ("You cannot divide the number by zero") message in this catch block. Okay, let's run the code. As you can see, we got the error message that we defined in the first catch block. Now, let's see how the program works.
The divide method is invoked first. The 8b in the first parseInt method is not a number format, so this method throws NumberFormatException. The first catch block handles this exception and the invalid number format message is displayed in the console. Let's change the value 8b to 8 in the first parseInt method to test the ArithmeticException. Okay, let's run the code again. As you can see, this time we got the message we defined in the second catch block. The divide method is invoked, and because the remainder is zero, ArithmeticException is thrown. The second catch block handles this exception and that you cannot divide the number by zero message is displayed in the console. So, that's how the throws keyword is used. Let's take a short break here. In our next lesson, we'll cover the difference between the throw and throws keywords. See you in the 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.