Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming / Difference Between throw and throws in Java

Difference Between throw and throws in Java

February 26, 2018 Posted by Lithmee

Key Difference – throw vs 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.

Difference Between throw and throws in Java

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.

Key Difference Between throw and throws in Java

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

Related posts:

Difference Between Prolog and Lisp Difference Between Tomcat 7.0 and Tomcat 6.0 Difference Between C and C# Difference Between Process and ThreadDifference Between Process and Thread Difference between OOP and POPDifference Between OOP and POP

Filed Under: Programming Tagged With: Compare throw and throws in Java, throw and throws in Java Differences, throw and throws in Java Similarities, throw in Java, throw in Java Definition, throw in Java Function, throw in Java Method, throw Method, throw vs throws in Java, throws in Java, throws in Java Definition, throws in Java Function, throws in Java Method

About the Author: Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Photoperiodism and Phototropism

Difference Between Photoperiodism and Phototropism

Difference Between Metal and Nonmetal Oxides

Difference Between Metal and Nonmetal Oxides

Difference Between Shares and Securities

Difference Between Shares and Securities

Difference Between Hearing and Trial

Difference Between Criteria and Criterion

Latest Posts

  • What is the Difference Between Argumentative and Expository Essay
  • What is the Difference Between Benzophenone-3 and Benzophenone-4
  • What is the Difference Between Lymphokines and Cytokines
  • What is the Difference Between Gastrolyte and Hydralyte
  • What is the Difference Between Avobenzone and Benzene
  • What is the Difference Between Tight Junction and Adherens Junction
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018 Difference Between. All rights reserved. Terms of Use and Privacy Policy: Legal.