
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Try, catch, throw and throws in Java
Try-catch, throw, and throws are keywords that are used for handling exceptions in Java. Exceptions are the problems that occur during the execution of a program.
When a problem occurs, the program you are trying to run will terminate abnormally. So, to handle these exceptions, Java has a mechanism called exception handling.
We handle different types of exceptions, such as runtime exceptions, compile-time exceptions, using try-catch, throw, and throws keywords.
Types of Exceptions
There are two types of exceptions in Java:
- Checked Exceptions: These are the exceptions that are checked at compile time. For example, ClassNotFountException, InstantiationException.
- Unchecked Exceptions: These are the exceptions that are not checked at compile time. For example, NullPointerException, ArithmeticException.
In this tutorial, we will learn about the try-catch, throw, and throws keywords in Java.
Try-catch
The try-catch block is used for handling exceptions. If you have a doubt on a piece of code that it may throw an exception, you can put that code in a try block. If an exception occurs, the catch block will handle it.
- We can have multiple catch blocks to handle different types of exceptions.
- The catch block will be executed only if an exception occurs in the try block.
- We can also have a finally block, which will always execute regardless of whether an exception occurred or not.
- But it is optional to have a finally block.
Syntax of try-catch
The syntax of the try-catch block is as follows:
try { // code that may throw an exception } catch (ExceptionType1 e1) { // handle exception of type ExceptionType1 } catch (ExceptionType2 e2) { // handle exception of type ExceptionType2 } finally { // code that will always execute, regardless of whether an exception occurred or not }
Example
For example, suppose you are trying to divide a number by zero. This will throw an ArithmeticException. So, we can put this code in a try block and handle the exception in the catch block. Following is the code snippet to demonstrate the try-catch block:
public class TryCatchExample { public static void main(String[] args) { int a = 10, b = 0; try { int c = a / b; // this will throw an ArithmeticException System.out.println("Result: " + c); } catch (ArithmeticException e) { System.out.println("Cannot divide by zero: " + e.getMessage()); } finally { System.out.println("This block will always execute."); } } }
Output
Cannot divide by zero: / by zero This block will always execute.
Throw
The throw is the keyword that is used for throwing an exception explicitly. We can use the throw keyword when we want to throw an exception manually. For example, if a user enters a negative number, we can throw an exception that says "Negative number not allowed".
We can throw any type of exception using the throw keyword.
Syntax of throw
The syntax of the throw keyword is as follows:
throw new ExceptionType("Exception message");
Example
Let's take an example where we will throw an exception if a user enters a negative number:
public class ThrowExample { public static void main(String[] args) { int number = -5; try { if (number < 0) { throw new Exception("Negative number not allowed"); } System.out.println("Number: " + number); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); } } }
Output
Exception: Negative number not allowed
Throws
The throws keyword is used in the method signature to declare that a method may throw one or more exceptions. It is used when we want to propagate an exception to the caller of the method. The caller of the method can then handle the exception using a try-catch block.
We can use the throws keyword to declare multiple exceptions separated by a comma.
Syntax of throws
The syntax of the throws keyword is as follows:
public void methodName() throws ExceptionType1, ExceptionType2 { // code that may throw an exception }
Example
For example, if a method may throw an NullPointerException and an ArithmeticException, we can declare it in the method signature using the throws keyword.
Following is the code snippet to demonstrate the usage of the throws keyword. In here, we have declared the method1() to throw NullPointerException and ArithmeticException. In the main() method, we are calling the method1() and handling the exceptions using a try-catch block.
public class ThrowsExample { public static void main(String[] args) { try { method1(); } catch (NullPointerException e) { System.out.println("NullPointerException: " + e.getMessage()); } catch (ArithmeticException e) { System.out.println("ArithmeticException: " + e.getMessage()); } } public static void method1() throws NullPointerException, ArithmeticException { String str = null; int a = 10, b = 0; if (str == null) { throw new NullPointerException("String is null"); } // This will throw an ArithmeticException int c = a / b; } }
Output
NullPointerException: String is null