Entri Blog
No Result
View All Result
Friday, August 12, 2022
  • State Level PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
  • SSC
  • Railway
  • Other Govt. Exam
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET
FREE GK TEST: SIGNUP NOW
Entri Blog
  • State Level PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
  • SSC
  • Railway
  • Other Govt. Exam
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET
No Result
View All Result
Entri Blog
Free GK Test
banner top article banner top article
Home Articles

Difference Between Throw and Throws in Java

by Shibani Ghosh
May 20, 2022
in Articles, Data Science and Machine Learning, Digital Marketing, Java Programming, React Native
Difference Between Throw and Throws in Java
Share on FacebookShare on WhatsAppShare on Telegram

Table of Contents

  • What is Throw in Java?
  • What is Throws in Java?

Talking about throw and throws, both are the concepts of exception handling. Throw is used to explicitly throw an exception from a method or any block of code. On the other hand, throws are used in the signature of the method to indicate that this method might throw one of the listed type exceptions.

What is Throw in Java?

Throw keyword in java is used to throw an exception explicitly and logically defined by the programmer during the program control moves from one block to another assuming the errors exception are defined and handled accordingly.

What is Throws in Java?

Throws is used to declare and call an exception block, which means its working is similar to the try-catch block.

Preparing for Data Science and Machine Learning? Check out Entri for guidance!

Differences between throw and throws

Let us look at table given below to have a clear look at what are the differences between throw and throws in Java:

Sr. no. Basis of Differences throw throws
1 Definition Java throw keyword is used throw an exception explicitly in the code, inside the function or the block of code.
Java throws keyword is used in the method signature to declare an exception which might be thrown by the function while the execution of the code.
2 Type of exception Using throw keyword, we can only propagate unchecked exception i.e., the checked exception cannot be propagated using throw only.
Using throws keyword, we can declare both checked and unchecked exceptions. However, the throws keyword can be used to propagate checked exceptions only.
3 Syntax The throw keyword is followed by an instance of Exception to be thrown.
The throws keyword is followed by class names of Exceptions to be thrown.
4 Declaration throw is used within the method.
throws is used with the method signature.
5 Internal implementation We are allowed to throw only one exception at a time i.e. we cannot throw multiple exceptions.
We can declare multiple exceptions using throws keyword that can be thrown by the method. For example, main() throws IOException, SQLException.

Subscribe to Entri for more details on Data Science and Machine Learning!

Examples for throw and throws

Check below for examples on throw and throws –

  • throw exception in java

// Java program to demonstrate the working
// of throw keyword in exception handling

public class GFG {
public static void main(String[] args)
{
// Use of unchecked Exception
try {
// double x=3/0;
throw new ArithmeticException();
}
catch (ArithmeticException e) {
e.printStackTrace();
}
}
}

Output –

java.lang.ArithmeticException
    at GFG.main(GFG.java:10)

Subscribe to Entri for more details on Data Science and Machine Learning!

  • throws keyword in java

// Java program to demonstrate the working
// of throws keyword in exception handling
import java.io.*;
import java.util.*;

public class GFG {

public static void writeToFile() throws Exception
{
BufferedWriter bw = new BufferedWriter(
new FileWriter(“myFile.txt”));
bw.write(“Test”);
bw.close();
}

public static void main(String[] args) throws Exception
{
try {
writeToFile();
}
catch (Exception e) {
e.printStackTrace();
}
}
}

 

Output –

java.security.AccessControlException: access denied ("java.io.FilePermission" "myFile.txt" "write")
  at GFG.writeToFile(GFG.java:10)

Prepare for your Data Science and Machine Learning with Entri!

You will be provided with an online platform to prepare for the exam. Entri provides video classes as well on various important topics by the excellent faculties. You can download the app free of cost and join the class. One will get revision modules, monthly tests based on the classes.

Share61SendShare
Shibani Ghosh

Shibani Ghosh

Pouring out my thoughts through words!

Related Posts

Delhi Police Driver Salary 2022: In Hand Salary, Pay Scale, Allowances
Articles

Delhi Police Driver Salary 2022: In Hand Salary, Pay Scale, Allowances

July 21, 2022
Top Applications of Data Science in E-commerce
Articles

Top Applications of Data Science in E-commerce

July 21, 2022
International Beer Day 2022- Theme, History, Interesting Facts
Articles

International Beer Day 2022- Theme, History, Interesting Facts

July 21, 2022
Next Post
Best Books to Prepare for MPPSC Dental Surgeon 2022 Exam

Best Books to Prepare for MPPSC Dental Surgeon 2022 Exam

Discussion about this post

Latest Posts

  • Kerala PSC Driver cum Office Attendant Syllabus and Exam Pattern 2022 – Check Exam Pattern, Download Syllabus
  • Haryana CET 2022 Exam Dates Out
  • What Is Data Mining? A Complete Guide
  • Kerala PSC Lab Assistant Syllabus and Exam Pattern 2022 – Check Exam Pattern, Download Syllabus
  • RRC WCR Recruitment 2022: Apply for 102 Posts

Trending Posts

  • states of india and their capitals and languages

    List of 28 States of India and their Capitals and Languages

    147279 shares
    Share 58909 Tweet 36818
  • List of Government Banks in India 2022: All you need to know

    58814 shares
    Share 23526 Tweet 14704
  • TNPSC Group 2 Posts and Salary Details 2022

    37207 shares
    Share 14883 Tweet 9302
  • Kerala Devaswom Board LDC Syllabus 2022 – Download PDF

    1305 shares
    Share 522 Tweet 326
  • New Map of India with States and Capitals 2022

    27990 shares
    Share 11196 Tweet 6997

Company

  • Become a teacher
  • Login to Entri Web

Quick Links

  • Articles
  • Videos
  • Entri Daily Quiz Practice
  • Current Affairs & GK
  • News Capsule – eBook
  • Preparation Tips
  • Kerala PSC Gold
  • Entri Skilling

Popular Exam

  • IBPS Exam
  • SBI Exam
  • Railway RRB Exam
  • Kerala PSC
  • Tamil Nadu PSC
  • Telangana PSC
  • Andhra Pradesh PSC
  • MPPSC
  • UPPSC
  • Karnataka PSC
  • Staff Selection Commission Exam

© 2021 Entri.app - Privacy Policy | Terms of Service

No Result
View All Result
  • State Level PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
  • SSC
  • Railway
  • Other Govt. Exam
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET

© 2021 Entri.app - Privacy Policy | Terms of Service

Try After Few Days!
30% OFF
Next time
Next time
Almost!
70% OFF
20% OFF
Next Time
Next time
Almost!
40% OFF
60% OFF
Unlucky
Spin the Wheel to Win FREE PREP on our courses! PSC, SSC, RRB, Banking, TET
Enter your email address and spin the wheel. This is your chance to win amazing discounts!
Our in-house rules:
  • One game per user
  • Cheaters will be disqualified.
  • Offer applicable for 1st time ENTRI users!