⬅ Previous Topic
Java throw KeywordNext Topic ⮕
Java Annotations⬅ Previous Topic
Java throw KeywordNext Topic ⮕
Java Annotationsthrows
Keyword in JavaThe throws
keyword in Java is used to declare exceptions. It tells the compiler that a method might throw one or more exceptions. This keyword doesn’t handle the exception directly; rather, it shifts the responsibility to the method caller.
Think of it as a way to say: “Hey, this method might cause a problem — be prepared to handle it!”
throws
in Java?In Java, when a method has a chance of throwing a checked exception (like IOException
or SQLException
), it must either:
try-catch
blockthrows
keywordFailing to do either results in a compilation error.
return_type method_name(parameters) throws ExceptionType1, ExceptionType2 {
// method body
}
import java.io.IOException;
public class FileProcessor {
public static void readFile() throws IOException {
throw new IOException("File not found!");
}
public static void main(String[] args) {
try {
readFile();
} catch (IOException e) {
System.out.println("Caught Exception: " + e.getMessage());
}
}
}
Caught Exception: File not found!
The readFile()
method declares that it might throw an IOException
. In the main()
method, we handle it using a try-catch
block. Without the throws
declaration, the compiler would complain.
import java.io.IOException;
import java.sql.SQLException;
public class MultiExceptionDemo {
public static void riskyMethod() throws IOException, SQLException {
if (System.currentTimeMillis() % 2 == 0) {
throw new IOException("IO problem");
} else {
throw new SQLException("DB problem");
}
}
public static void main(String[] args) {
try {
riskyMethod();
} catch (IOException | SQLException e) {
System.out.println("Caught Exception: " + e.getMessage());
}
}
}
Caught Exception: IO problem
// or
Caught Exception: DB problem
This example shows how a method can declare multiple exceptions using throws
. Depending on the condition, either IOException
or SQLException
is thrown. The catch
block handles both types using multi-catch (|
syntax).
import java.io.*;
public class PropagateDemo {
public static void method1() throws IOException {
throw new IOException("Thrown from method1");
}
public static void method2() throws IOException {
method1(); // propagating the exception
}
public static void main(String[] args) {
try {
method2();
} catch (IOException e) {
System.out.println("Caught Exception: " + e.getMessage());
}
}
}
Caught Exception: Thrown from method1
Here, method1()
throws an exception. Instead of handling it inside method2()
, we use throws
to let it bubble up. Finally, main()
handles it. This is known as exception propagation.
throws
IOException
) must be either handled or declared with throws
.NullPointerException
) don’t require declaration.Imagine a pizza delivery driver gets stuck in traffic. Instead of calling every customer to say, “I’ll be late,” they notify the manager (the calling method), who then handles the customer interactions. This is what throws
does — it delegates responsibility upward.
throws Exception
— be specific about what can go wrong.throws
when the method really can’t or shouldn’t handle the exception.@throws
).The throws
keyword is your tool for delegation in Java’s exception ecosystem. It empowers methods to signal problems without dictating how they should be handled. In production-grade systems, clean exception propagation using throws
ensures modular and testable code.
Up next, you’ll learn about throw
— the partner of throws
— to actually generate exceptions when needed.
⬅ Previous Topic
Java throw KeywordNext Topic ⮕
Java AnnotationsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.