Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

In Java, the keywords throw and throws are used in exception handling to manage errors and other exceptional events in a program. They are part of Java’s mechanism to handle runtime errors and ensure a smooth execution flow.

throw Keyword

The throw keyword is used to explicitly throw an exception from a method or a block of code. When a throw statement is executed, the normal flow of the program is disrupted, and the control is transferred to the nearest enclosing try-catch block that can handle the thrown exception.

Syntax:

				
					throw new ExceptionType("Error message");

				
			

Example:

				
					public class ThrowExample {
    public static void main(String[] args) {
        try {
            validateAge(15);
        } catch (Exception e) {
            System.out.println("Exception caught: " + e.getMessage());
        }
    }

    static void validateAge(int age) {
        if (age < 18) {
            throw new IllegalArgumentException("Age must be 18 or older");
        } else {
            System.out.println("Age is valid");
        }
    }
}

				
			

Explanation:

  • The validateAge method checks the age.
  • If the age is less than 18, it throws an IllegalArgumentException.
  • The main method calls validateAge inside a try-catch block to handle the exception.

throws Keyword

The ‘throws‘ keyword is used in the method signature to declare that a method might throw one or more exceptions. It informs the caller of the method about the potential exceptions that the method can throw, and it is the caller’s responsibility to handle those exceptions.

Syntax:

				
					returnType methodName(parameters) throws ExceptionType1, ExceptionType2 {
    // method body
}

				
			

Example:

				
					import java.io.IOException;

public class ThrowsExample {
    public static void main(String[] args) {
        try {
            readFile("nonexistentfile.txt");
        } catch (IOException e) {
            System.out.println("Exception caught: " + e.getMessage());
        }
    }

    static void readFile(String fileName) throws IOException {
        if (fileName.equals("nonexistentfile.txt")) {
            throw new IOException("File not found");
        }
    }
}

				
			

Explanation:

  • The readFile method declares that it throws an IOException.
  • When readFile is called with an invalid file name, it throws an IOException.
  • The main method calls readFile inside a try-catch block to handle the exception.

Key Differences

1.Usage Context:

  • throw is used within a method to throw an exception.
  • throws is used in a method signature to declare that the method can throw specified exceptions.

2. Purpose:

  • throw actually triggers an exception.
  • throws provides information to the caller about the possible exceptions that could be thrown by the method.

3. Scope:

  • throw is followed by an instance of an exception.
  • throws is followed by exception class names separated by commas.

4. Location:

  • throw is used inside method bodies.
  • throws is used in method declarations.

Understanding how to use throw and throws effectively allows developers to handle exceptions in a controlled and predictable manner, making Java applications more robust and reliable.

Scroll to Top