Java 'throws' Keyword Tutorial

The throws keyword is used to declare an exception. It gives an information to the programmer that there may occur an exception. So, it is better for the programmer to provide the exception handling code so that normal flow can be maintained.

Usage

It is always used with method signature.

type method_name() throws exception_class_name {
  //...
}

Example

import java.io.IOException;

class Testthrows1 {
  void m() throws IOException {
    throw new IOException("device error"); //checked exception
  }
  
  void n() throws IOException {
    m();
  }
  
  void p() {
   try {
    n();
   } catch(Exception e) { System.out.println("exception handled"); }
  }
  
  public static void main(String args[]) {
   Testthrows1 obj=new Testthrows1();
   obj.p();
   System.out.println("normal flow...");
  }
}

Frequently Asked Questions

What will I learn here?

This page covers the core concepts and techniques you need to understand the topic and progress confidently to the next lesson.

How should I use this page?

Start with the overview, then follow the section links to deepen your understanding. Use the table of contents on the right to jump to specific sections.

What should I read next?

Use the navigation below to continue to the next lesson or explore related topics.