CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Exception Handling Keywords in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

Exception Handling Keywords in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 4 min read ยท 0 Comments
Share: in X
Exception Handling Keywords in Java

Exception handling is a crucial aspect of writing robust Java applications, ensuring that programs can gracefully handle runtime errors without crashing. In Java, there are 5 exception handling keywords such as try, catch, finally, throw, and throws. These keywords allow developers to manage errors efficiently, define custom exceptions, and maintain code stability. In this blog post, we will explore these keywords in detail, understand their use cases, and demonstrate their implementation with practical examples.

Table of Contents

  • 5 Exception Handling Keywords in Java
    • try
    • catch
    • finally
    • throw
    • throws
  • Conclusion
  • FAQs
    • What is the try keyword used for in Java?
    • What is the purpose of the catch keyword in Java?
    • How do I specify the type of exception to catch using the catch keyword?
    • What is the purpose of the throws keyword in Java method declarations?
    • Can you have multiple catch blocks after a single try block?
    • When is the finally block executed in Java’s exception handling?
    • What is the difference between throw and throws in Java?

5 Exception Handling Keywords in Java

try

The try keyword is used to enclose a block of code that may throw an exception. If an exception is thrown inside the try block, the catch block will be executed.

try {
  // Code that may throw an exception
} catch (Exception e) {
  // Handle the exception
}

catch

The catch keyword is used to catch an exception that is thrown by the try block. The catch block must be followed by a parameter that specifies the type of exception that it can handle.

try {
  // Code that may throw an exception
} catch (ArithmeticException e) {
  // Handle the ArithmeticException exception
}

finally

The finally keyword is used to execute a block of code regardless of whether or not an exception is thrown. The finally block is often used to close resources, such as files and sockets.

try {
  // Code that may throw an exception
} catch (Exception e) {
  // Handle the exception
} finally {
  // Close resources
}

Example to show try, catch, and finally keywords used together.

public class ExceptionHandlingDemo {

  public static void main(String[] args) {
    try {

      int x = 10;
      int y = 0;
      // The following will throw an ArithmeticException exception
      System.out.println(x / y);
    } catch (ArithmeticException e) {

      System.out.println("Cannot divide by zero");
    } finally {

      System.out.println("Closing resources");
    }
  }
}

When we run this code, it will print the following output:

Cannot divide by zero
Closing resources

throw

The throw keyword is used to throw an exception. The throw keyword must be followed by an object of the Exception class or a subclass of the Exception class.

public void divideByZero() throws ArithmeticException {
  int x = 10;
  int y = 0;

  // Throw an ArithmeticException exception if y is 0
  if (y == 0) {
    throw new ArithmeticException("Cannot divide by zero");
  }
}

throws

The throws keyword is used to declare that a method may throw an exception. The throws keyword must be followed by a list of the exceptions that the method may throw.

public int divide(int x, int y) throws ArithmeticException {
  // Code that may throw an ArithmeticException exception

  return x / y;
}

Conclusion

Effective exception handling is essential for building reliable Java applications. By using exception handling keywords in Java like try, catch, finally, throw, and throws, developers can manage errors gracefully and ensure smooth program execution. Mastering these concepts will help you write more robust and maintainable Java code.

FAQs

What is the try keyword used for in Java?

The try keyword is used to enclose a block of code where exceptions may occur. It allows you to specify a portion of your code that needs error handling.

What is the purpose of the catch keyword in Java?

The catch keyword is used to define a block of code that handles exceptions. When an exception occurs in the associated try block, the code in the catch block is executed to handle the exception. And we can write our code that needs to be executed when this exception is occured.

How do I specify the type of exception to catch using the catch keyword?

You specify the type of exception to catch by including the exception’s class in parentheses after the catch keyword.
For example: catch (ExceptionType e){}.

What is the purpose of the throws keyword in Java method declarations?

The throws keyword is used in a method declaration to indicate that the method might throw a particular type of exception. It informs callers of the method about the exceptions they should handle or propagate.

Can you have multiple catch blocks after a single try block?

Yes, you can have multiple catch blocks following a single try block in Java. Each catch block can handle a specific type of exception, allowing you to handle different exceptions differently.

When is the finally block executed in Java’s exception handling?

The finally block is executed regardless of whether an exception occurred or not in the associated try block. It is the last block to execute in an exception-handling sequence.

What is the difference between throw and throws in Java?

throw is used to manually throw an exception within our code, while throws is used in method declarations to declare the types of exceptions that the method may throw.

Related Posts:

  • Exception Handling in Java
  • Suppressed Exceptions in Java
  • User Defined Exception in Java
  • Top 10 Common Java Errors
  • Control Statements in Java
  • MySQL Commands for Developers
Tags:exceptionjavalanguage-fundamentals
Was this article helpful?
โ† Previous ArticleExtends Keyword in Java: A Deep Dive into Inheritance
Next Article โ†’User Defined Exception in Java

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap