Java Programming Handbook

    Throw vs Throws in Java

    Introduction#

    When handling exceptions in Java, developers often come across the throw and throws keywords. Though they look similar, their usage and purpose are entirely different. In this blog, we will explore:

    • The difference between throw and throws
    • When to use each
    • Examples illustrating their usage
    • Best practices

    Understanding throw#

    The throw keyword is used inside a method to explicitly throw an exception. It allows us to create and throw both checked and unchecked exceptions manually.

    Syntax:#

    throw new ExceptionType("Error message");

    Example of throw:#

    public class ThrowExample { public static void validateAge(int age) { if (age < 18) { throw new IllegalArgumentException("Age must be 18 or above"); } System.out.println("Valid age"); } public static void main(String[] args) { validateAge(15); // This will throw an exception } }

    Output:#

    Exception in thread "main" java.lang.IllegalArgumentException: Age must be 18 or above at ThrowExample.validateAge(ThrowExample.java:4) at ThrowExample.main(ThrowExample.java:9)

    Explanation: The throw keyword is used inside validateAge() to manually throw an IllegalArgumentException if the age is below 18.

    Understanding throws#

    The throws keyword is used in a method declaration to specify that the method might throw an exception. This allows the caller of the method to handle the exception.

    Syntax:#

    returnType methodName(parameters) throws ExceptionType { // Method body }

    Example of throws:#

    import java.io.IOException; public class ThrowsExample { 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("Exception caught: " + e.getMessage()); } } }

    Output:#

    Exception caught: File not found

    Explanation: Here, readFile() declares that it throws an IOException. The calling method (main) must handle it using a try-catch block.

    Key Differences Between throw and throws#

    Featurethrowthrows
    PurposeUsed to explicitly throw an exceptionDeclares that a method might throw exceptions
    PlacementInside a method bodyIn the method signature
    TypeUsed for both checked and unchecked exceptionsMainly used for checked exceptions
    HandlingException must be caught or declared by the callerCaller must handle the exception

    Example: throw vs throws#

    import java.io.IOException; public class ThrowVsThrowsExample { // Using throws in method declaration public static void riskyMethod() throws IOException { throw new IOException("Risky operation failed"); } public static void main(String[] args) { try { riskyMethod(); } catch (IOException e) { System.out.println("Exception handled: " + e.getMessage()); } } }

    Output:#

    Exception handled: Risky operation failed

    Best Practices#

    • Use throw when you want to manually trigger exceptions.
    • Use throws to indicate that a method may throw exceptions, especially for checked exceptions.
    • Handle exceptions properly using try-catch when calling methods that declare throws.
    • Avoid declaring throws Exception unless necessary; instead, specify the actual exception type.

    Conclusion#

    In this blog, we covered:

    • The difference between throw and throws
    • When and how to use them
    • Examples of their implementation
    • Best practices for handling exceptions

    Next, we will explore Finally Block in Java, which ensures code execution regardless of exceptions.

    Last updated on Apr 09, 2025