- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between throw and throws in Java
Both throw and throws are the concepts of exception handing in which throw is used to explicitly throw an exception from a method or any block of code while throws are used in the signature of the method to indicate that this method might throw one of the listed type exceptions.
The following are the important differences between throw and throws.
Sr. No. | Key | throw | throws |
---|---|---|---|
1 | Definition | Throw is a keyword which is used to throw an exception explicitly in the program inside a function or inside a block of code. | Throws is a keyword used in the method signature used to declare an exception which might get thrown by the function while executing the code. |
2 | Internal implementation | Internally throw is implemented as it is allowed to throw only single exception at a time i.e we cannot throw multiple exception with throw keyword. | On other hand we can declare multiple exceptions with throws keyword that could get thrown by the function where throws keyword is used. |
3 | Type of exception | With throw keyword we can propagate only unchecked exception i.e checked exception cannot be propagated using throw. | On other hand with throws keyword both checked and unchecked exceptions can be declared and for the propagation checked exception must use throws keyword followed by specific exception class name. |
4 | Syntax | Syntax wise throw keyword is followed by the instance variable. | On other hand syntax wise throws keyword is followed by exception class names. |
5 | Declaration | In order to use throw keyword we should know that throw keyword is used within the method. | On other hand throws keyword is used with the method signature. |
Example of throw vs throws
JavaTester.java
public class JavaTester{ public void checkAge(int age){ if(age<18) throw new ArithmeticException("Not Eligible for voting"); else System.out.println("Eligible for voting"); } public static void main(String args[]){ JavaTester obj = new JavaTester(); obj.checkAge(13); System.out.println("End Of Program"); } }
Output
Exception in thread "main" java.lang.ArithmeticException: Not Eligible for voting at JavaTester.checkAge(JavaTester.java:4) at JavaTester.main(JavaTester.java:10)
Example
JavaTester.java
public class JavaTester{ public int division(int a, int b) throws ArithmeticException{ int t = a/b; return t; } public static void main(String args[]){ JavaTester obj = new JavaTester(); try{ System.out.println(obj.division(15,0)); } catch(ArithmeticException e){ System.out.println("You shouldn't divide number by zero"); } } }
Output
You shouldn't divide number by zero
- Related Articles
- What is the difference between throw and throws keywords in Java?
- Throw and throws in Java
- Try, catch, throw and throws in Java
- What is the difference between throw e and throw new Exception(e) in catch block in java?
- Is it possible to throw exception without using "throws Exception" in java?
- What is the difference between 'throw new Error' and 'throw someObject' in javascript?
- Can constructor throw exceptions in Java?
- Difference between Java and JavaScript.
- Difference between Go and Java.
- Difference Between C++ and Java
- Difference between Groovy and Java
- Difference between constructor and method in Java
- Difference between Object and Class in Java
- Difference between HashMap and HashTable in Java.
- Difference between StringBuilder and StringBuffer in Java

Advertisements