Throws Keyword

Start course
Overview
Difficulty
Beginner
Duration
50m
Students
24
Description

In this course, we'll learn about Exception Handling in Java.

Learning Objectives

  • What is an Exception?
  • Difference between Error and Exception
  • Types of Exceptions
  • Try-Catch Block
  • Finally Block
  • Throw and Throws Keywords
  • Exception Methods

Intended Audience

  • 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

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. In this video, we'll 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 caller method might handle these exceptions with a 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 NumbersFormatException 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 in a number format and the divider is equal to zero, this time it throws an ArithmeticException. 

To better understand the use of the throws keyword, let's move on to the Eclipse and make an example. In ExceptionHandling 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 the value 0 to it. The parseInt method is used for parsing a string argument to an integer object. I will declare the last variable int c and assign a / b to it. And I'll display the value of variable c by using the print method. S.out.println (" Result : " + c); and I'll 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 the 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. In this catch block, we display the "You cannot divide the number by zero" message by using the print method. 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" with 8 in the first parseInt method to test the ArithmeticException. 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 the "You cannot divide the number by zero" message is displayed in the console. Yes, 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 the throws keywords. See you in the next lesson.

 

About the Author
Students
512
Courses
47
Learning Paths
3

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.

Covered Topics