Difference Between throw and throws in Java

There can be mistakes when programming. A mistake in the program gives an unexpected result or it can terminate the execution of the program. Therefore, it is better to detect and manage the errors properly to execute the program correctly. An error can be of two types. They are the compile-time errors and runtime errors. When there are syntax errors, there are indicated by the Java compiler. Those are called compile-time errors. Some common compile-time errors are missing semicolon, missing curly braces, undeclared variables and misspelling identifiers or keywords. Sometimes, the program can compile properly but it can give wrong output. They are called runtime errors. Some common runtime errors are dividing by zero and assessing an element that is out of bound of an array. An exception is a condition caused by a runtime error in the program. Program execution terminates when an exception occurs. If the programmer wants to continue the execution of the remaining code, then the programmer can catch the exception object thrown by the error condition and display an error message. This is called exception handling. The code that can cause an error is place in try block and the message is in the catch block. The throw and throws are two keywords used in Java exception handling.The key difference between throw and throws in Java is at, throw is a keyword used to explicitly throw an exception while throws is used to declare an exception.

CONTENTS

1. Overview and Key Difference
2. What is throw in Java
3. What is throws in Java
4. Similarities Between throw and throws in Java
5. Side by Side Comparison – throw vs throws in Java in Tabular Form
6. Summary

What is throw in Java?

The keyword throw is used to throw an exception explicitly. The throw is followed by an instance of Exception class. e.g. – throw new Exception (“Error divide by zero”); It is used inside the method body to throw an exception.  Refer the below program.

Figure 01: Program with throw keyword

According to the above program, the Exception3 class has a method called checkMarks. If the marks are less than 50, it will cause an exception and display “Fail”. If the marks are higher than or equal to 50, it will print the message “Pass”.

What is throws in Java?

The throws keyword is used to declare an exception. It is followed by the exception class name. e.g. – throws Exception. The programmer can declare multiple exceptions using the throws keyword.  It is used with method signature. Refer the below example.

Figure 02: Program with throws keyword

The code which can have an error is placed inside the try black. The error message is inside the catch block. The method caller identifies that certain types of exceptions could be expected from the called method. The caller should be prepared with some catching mechanism. In this situation, the throws keyword is used. It is specified immediately after the method declaration statement and just before the opening brace.

What is the Similarity Between throw and throws in Java?

  • Both are keywords in Java for exception handling.

What is the Difference Between throw and throws in Java?

throw vs throws in Java

The ‘throw’ is a keyword in Java that is used to explicitly throw an exception. The ‘throws’ is a keyword in Java that is used to declare an exception.
 Multiple Exception
There cannot be multiple exceptions with throw. There can be multiple exceptions with throws.
Followed By
The ‘throw’ is followed by an instance. The ‘throws’ is followed by the class.
 Method of Using
The ‘throw’ is used within the method. The ‘throws’ is used with method signature.

Summary – throw vs throws in Java 

Run time errors cause the program to compile but it gives unexpected results or terminates the execution of the program. That condition is an exception. The throw and throws are two keywords used in Java programming for exception handling. This article discussed the difference between throw and throws. The difference between throw and throws in Java is that throw is a keyword used to explicitly throw an exception while throws is used to declare an exception.

Reference:

1.Throw and Throws Keyword in java with Example | Exception Handling Java Theory, Programming Tube, 2 Nov. 2017. Available here
2.Point, Tutorials. “Java Exceptions.”  Tutorials Point, 8 Jan. 2018. Available here
3.“Java Throws Keyword – javatpoint.” Tutorials Point, Available here