BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Throws Keyword in Exception handling

Last Updated: September 14, 2022 by Chaitanya Singh | Filed Under: java

The throws keyword is used to handle checked exceptions. As we learned in the previous article that exceptions are of two types: checked and unchecked. Checked exception (compile time) needs to be handled else the program won’t compile. On the other hand unchecked exception (Runtime) doesn’t get checked during compilation. Throws keyword is used for handling checked exceptions. You can declare multiple exceptions using throws keyword.

The throws keyword vs try-catch in Java

You may be wondering why we need throws keyword when we can handle exceptions using try-catch block in Java. Well, thats a valid question. We already know we can handle exceptions using try-catch block.

The throws keyword does the same thing that try-catch does but there are some cases where you would prefer throws over try-catch. For example: Lets say we have a method myMethod() the statements inside this method can throw either ArithmeticException or NullPointerException, in this case you can use try-catch as shown below:

public void myMethod()
{
  try {
    // Statements that might throw an exception 
  }
  catch (ArithmeticException e) {
    // Exception handling statements
  }
  catch (NullPointerException e) {
    // Exception handling statements
  }
}

But suppose you have several such methods that can cause exceptions, in that case it would be tedious to write these try-catch for each method. The code will become unnecessary long and will be less-readable.

One way to overcome this problem is by using throws like this: declare the exceptions in the method signature using throws and handle the exceptions where you are calling this method by using try-catch.

Another advantage of using this approach is that you will be forced to handle the exception when you call this method, all the exceptions that are declared using throws, must be handled where you are calling this method else you will get compilation error.

public void myMethod() throws ArithmeticException, NullPointerException
{
  // Statements that might throw an exception 
}

public static void main(String args[]) { 
  try {
    myMethod();
  }
  catch (ArithmeticException e) {
    // Exception handling statements
  }
  catch (NullPointerException e) {
    // Exception handling statements
  }
}

Example of throws Keyword

In this example the method myMethod() is throwing two checked exceptions so we have declared these exceptions in the method signature using throws Keyword. If we do not declare these exceptions then the program will throw a compilation error.

import java.io.*;
class ThrowExample { 
  void myMethod(int num)throws IOException, ClassNotFoundException{ 
     if(num==1)
        throw new IOException("IOException Occurred");
     else
        throw new ClassNotFoundException("ClassNotFoundException");
  } 
} 

public class Example1{ 
  public static void main(String args[]){ 
   try{ 
     ThrowExample obj=new ThrowExample(); 
     obj.myMethod(1); 
   }catch(Exception ex){
     System.out.println(ex);
    } 
  }
}

Output:

java.io.IOException: IOException Occurred

For more examples on throws refer this tutorial: throws examples.

❮ PreviousNext ❯

Top Related Articles:

  1. Java Exception Handling Examples
  2. Exception handling in Java with examples
  3. Constructor Overloading in Java with examples
  4. Nested try catch block in Java – Exception handling
  5. Throw Keyword in Java with Examples

Tags: Exception-Handling

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. gowthami says

    June 30, 2014 at 5:37 AM

    i want to know the difference between checked and unchecked exception?
    then how to rethrow an exception?
    why we are rethrowing an exception?
    explain with suitable exaple

    Reply
  2. yu song says

    November 11, 2015 at 2:52 AM

    This is a real excellence website for Java beginners. it makes the concepts easy to understand. and also provide the simple examples to tell how to use them. i appreciate the writer/web owner’s effort. Thanks.

    Reply
  3. priyanka duggirala says

    April 1, 2016 at 3:32 AM

    you are superb sir. I can easily understand the concepts I felt very difficult till now. I cannot express my happiness when I understand the concepts.
    Thank you so much

    Reply
  4. rossi says

    April 11, 2016 at 10:56 PM

    Output is Null pointer Exception in the full THROWS Example

    Reply
  5. Santhosh says

    April 23, 2016 at 4:06 AM

    The best site for learning. Good stuff with easy explanation.

    Reply
  6. Murali Bojanki@cts says

    April 28, 2016 at 8:47 AM

    Really Awesome It is Good to Understanding Everyone if we know bit knowledge in java concepts, really wonderful and helful to every one.

    Reply

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap