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

Examples of throws Keyword in Java

By Chaitanya Singh | Filed Under: java

In this guide, we will see few examples of throws keyword. I highly recommend you to read my detailed guide on throws keyword before going through these examples so that you have a better understand of the this concept.

Read these guides to learn exception handling from scratch:

  • Exception handling in Java – complete guide
  • Exception handling examples

Example 1: Exception propagation using throws keyword

In this example we are seeing an example of throws keyword in exception propagation. Here, an exception occurred in method1() which has been handled in the chain-calling method method3().

This example shows how exception propagation works. If the exception is not handled in the method then compiler checks whether the exception is handled in the calling method.

Here compiler didn’t find exception handling in method1(), so it checked in the calling method method2(), then it checked in the calling method method3(), where the exception is handled. The exception propagation concept is explained in detail here.

class Example1{  
   void method1() throws ArithmeticException{  
	throw new ArithmeticException("Calculation error"); 
   }  
   void method2() throws ArithmeticException{  
	method1();  
   }  
   void method3(){  
	try{  
	   method2();  
	}
	catch(ArithmeticException e){
	   System.out.println("ArithmeticException handled");
	}  
   }  
   public static void main(String args[]){  
	Example1 obj=new Example1();  
	obj.method3();  
	System.out.println("End Of Program");  
   }  
}

Output:

ArithmeticException handled
End Of Program

Example 2: throw and throws keyword example

In this example, we are using both throw keyword and throws keyword. We have declared the IOException using throws keyword and using the throw keyword to raise the exception in the myMethod().

import java.io.*;
class Demo{
  void myMethod()throws IOException{
    //throw exception using throw keyword
    throw new IOException("IO Exception occurred");
  }
}
class JavaExample{
  //declared IOException using throws keyword
  public static void main(String args[])throws IOException{
    Demo obj = new Demo();
    obj.myMethod();

    System.out.println("Rest of the program");
  }
}

Output:
throws example

Example 3: When you don’t handle exception that is declared using throws

The ideal way to use throws is by declaring the exceptions in method signature and handle the exceptions using try-catch in calling method. Let’s see what happens when we declare the exception at both the places, in method signature as well as in calling method.

class ExceptionExample{  
 void method()throws ArithmeticException{  
  throw new ArithmeticException("ArithmeticException Occurred");  
 }  
}  
class Example1{  
   public static void main(String args[])throws ArithmeticException{
     ExceptionExample obj=new ExceptionExample();  
     obj.method();  
  
    System.out.println("End Of Program");  
  }  
}

Output:

Exception in thread "main" java.lang.ArithmeticException: 
ArithmeticException Occurred
at ExceptionExample.method(Example1.java:4)
at Example1.main(Example1.java:10)
❮ PreviousNext ❯

Comments

  1. anand says

    August 15, 2016 at 12:46 PM

    can we declare unchecked exception using throws keyword?

    Reply
    • Ajeet kumar says

      May 18, 2017 at 9:16 AM

      no you can’t

      Reply
  2. Cheta says

    June 23, 2017 at 10:27 AM

    I am trying to write the same code within same class. how come it’s going to catch block every time?

    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 – 2022 BeginnersBook . Privacy Policy . Sitemap