CodeGym /Courses /JAVA 25 SELF /Propagating exceptions (throws)

Propagating exceptions (throws)

JAVA 25 SELF
Level 11, Lesson 4
Available

1. Introduction

In programming, it is common to encounter situations where a method runs into an error but does not know how to handle it properly. For example, a method reads a file but does not know what to do if the file is missing: ask the user? Exit the program? Try another file? In such cases, the method can “shift the responsibility” to its caller — that is, “propagate” the exception further up the call chain.

Exception propagation is a mechanism that allows a method not to handle an error itself, but to inform the calling code: “I’ve got a problem here — you decide what to do!”

Analogy: Imagine you’re a call center employee. A customer calls with a question you don’t know the answer to. Instead of guessing, you say: “One moment, I’ll connect you with a specialist.” You “propagate” the question further.

The throws keyword: how it works

In Java, you use the throws keyword in a method declaration to propagate exceptions.

Syntax:

return_type methodName(...) throws ExceptionType 
{
    // method code
}

After throws you specify the type of exception that might occur in this method. It’s like a warning to other developers: “Attention! This method can throw an exception of this type. Be prepared!”

Example:

public void readFile(String filename) throws FileNotFoundException 
{
    FileReader reader = new FileReader(filename); // may throw FileNotFoundException
    // ...
}

Here, the readFile method does not handle the error itself; it says: “I can throw FileNotFoundException — let whoever called me decide what to do.”

2. How should the caller respond?

If you call a method that is declared with throws, you have two options:

  • Handle the exception with try-catch
  • Propagate the exception further (add throws to your own method)

Option 1: handle with try-catch

public static void main(String[] args) 
{
    try 
    {
        readFile("data.txt");
    } 
    catch (FileNotFoundException e) 
    {
        System.out.println("File not found: " + e.getMessage());
    }
}

Here we catch the exception and decide what to do ourselves (for example, show a message to the user).

Option 2: propagate further

public static void main(String[] args) throws FileNotFoundException 
{
    readFile("data.txt");
}

Now the responsibility for handling the error lies with whoever calls main (usually the JVM itself — if the error is not handled, the program will exit with an error message).

3. Example: reading a file with exception propagation

A complete example with IOException and handling in main:

import java.io.*;

public class FileDemo 
{
    // The method declares that it may throw IOException
    public static void printFirstLine(String filename) throws IOException 
    {
        BufferedReader reader = new BufferedReader(new FileReader(filename));
        String line = reader.readLine();
        System.out.println("First line: " + line);
        reader.close();
    }

    public static void main(String[] args) 
    {
        try 
        {
            printFirstLine("nofile.txt");
        } 
        catch (IOException e) 
        {
            System.out.println("Error reading file: " + e.getMessage());
        }
    }
}

The printFirstLine method does not know what to do if the file is missing — it simply propagates the exception. In main we catch the error and print a message.

4. Useful details

When and why to use exception propagation?

  • When a method cannot or should not decide how to handle an error (for example, library code).
  • When error handling depends on context (in one case exit the program, in another — try a different file).
  • To avoid cluttering the code with unnecessary try-catch where it isn’t needed.

Best practice: Propagate exceptions if you cannot handle them meaningfully. Don’t catch exceptions just for the sake of it!

You can declare multiple exceptions

public void process() throws IOException, SQLException 
{
    // ...
}

Checked vs unchecked exceptions: a reminder

Checked exceptions (for example, IOException, SQLException) — the compiler requires them to be handled or declared.

Unchecked exceptions (for example, NullPointerException, IllegalArgumentException) — the compiler does not require handling; you do not need to list them in throws.

5. Common mistakes when propagating exceptions

Error No. 1: forgot to declare throws for a checked exception.
If a method can throw a checked exception but you neither declare it in throws nor handle it with try-catch, the compiler will report an error.

Error No. 2: catching an exception but not handling it.
Writing catch (Exception e) {} is bad practice! It’s better to propagate the exception if you don’t know what to do with it.

Error No. 3: declaring an overly general type.
If a method can only throw FileNotFoundException, don’t write throws Exception — it makes the code harder to understand.

Error No. 4: listing unchecked exceptions in throws.
There’s no point in writing throws NullPointerException — the compiler doesn’t require it, and it doesn’t help with error handling.

1
Task
JAVA 25 SELF, level 11, lesson 4
Locked
Secret Document Access Module 📁
Secret Document Access Module 📁
1
Task
JAVA 25 SELF, level 11, lesson 4
Locked
Log Analysis Tool: Handling Read Failures 📊
Log Analysis Tool: Handling Read Failures 📊
1
Task
JAVA 25 SELF, level 11, lesson 4
Locked
Data processing pipeline: expected file issues ⚙️
Data processing pipeline: expected file issues ⚙️
1
Task
JAVA 25 SELF, level 11, lesson 4
Locked
Gold Rush: converting player input 💰
Gold Rush: converting player input 💰
Comments (1)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Sid Level 1, United States of America, United States
22 March 2026
and where is this explained ? if(args.length>0){ accessSecretDocument(args[0]);