Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Try Block?
2.1.
Syntax of Try Block
3.
What is a Catch Block?
3.1.
Syntax of a catch block
4.
Internal working of Java try-catch block
4.1.
Example
4.1.1.
Implementation 1
4.1.2.
Implementation 2
5.
Frequently Asked Questions
5.1.
What are the advantages of Java to try to catch block statement?
5.2.
Can we handle errors in a catch block?
5.3.
What is the difference between try-catch and throws in Java?
6.
Conclusion
Last Updated: Sep 7, 2024

Java Try catch block

Author Mohammad Saalim
2 upvotes

Introduction

The try-catch block contains a set of statements where an exception occurs. A catch block always comes after a try block, which handles the exception inside a try block. 

Java Try catch block

Must Read, Multithreading in Java  

What is Try Block?

A block of code encloses the code, which might give an exception. It should be defined within a function. 

If an exception has occurred at the particular statement in the try block, the rest of the block code will not execute. A try block must be followed by either catch block, finally block, or both. 

Syntax of Try Block

try {
	// a set of statements that may cause an exception.
}
You can also try this code with Online Java Compiler
Run Code


Also see,  Swap Function in Java

What is a Catch Block?

A catch block is a block where you handle the exceptions. It handles the exception by declaring the type of exception within the parameter. You can catch different exceptions in different catch blocks. You can use multiple catch blocks with a single try block.

Syntax of a catch block

try {
	// statement might cause an exception
}
catch(exception(type) e (object))
{
	//error handling code
}
You can also try this code with Online Java Compiler
Run Code

Internal working of Java try-catch block

The JVM firstly checks whether the exception can be handled or not. If it cannot be dealt with, JVM gives a default exception handler that performs the following tasks:

  • It prints out the exception description.
  • It prints the stack trace (Hierarchy of methods where the exception occurred).
  • It causes the code to terminate.

Example

Implementation 1

public class Ex {
	public static void main(String[] args) {
		int data = 10/0; // it will throw an exception.
		System.out.println("rest of the code");
	}
}
You can also try this code with Online Java Compiler
Run Code

Output:

Exception in thread "main" java.lang.ArithmeticException: / by zero
at CODING/CN.Ex.main(Ex.java:7)
You can also try this code with Online Java Compiler
Run Code


In this example, we simply divide the number by zero. In the output section, we have received an exception message in output section that this number cannot be divided by zero.

Try it on online java compiler.

Implementation 2

public class Ex8 {
	public static void main(String[] args){
		try
		{
    		int data = 100/0; // may throw an exception
		}
		//handling exception
		catch(ArithmeticException e)
		{
  			System.out.println(e);
    	}
    	System.out.println("rest of the code");
    }
}
You can also try this code with Online Java Compiler
Run Code

Output:

java.lang.ArithmeticException: / by zero
rest of the code
You can also try this code with Online Java Compiler
Run Code


In this code, the section we applied some arithmetic to divide the number by zero. In this section, we have used the try-catch block statement. In the try block, we received some arithmetic operation and was the send further block to an exception to catch it. This will help you to execute the further part of the code as it is. 

Must Read Static Blocks In Java and Hashcode Method in Java

Frequently Asked Questions

What are the advantages of Java to try to catch block statement?

The use of try/catch block is used to differentiate between the code showing the error and showing the correct output, thereby maintaining the logical flow of the program. 

Can we handle errors in a catch block?

Yes, we can, with the help of the throwable class, which is the superclass of all the exceptions in the Java language. Java Virtual Machine throws only the objects that are instances of this class.

What is the difference between try-catch and throws in Java?

In Java, try-catch blocks are used to handle exceptions by wrapping code that might throw an exception. Conversely, the throws keyword is used in a method signature to indicate that a method might throw specific exceptions to its caller.

Conclusion

In this article, we talked about the Java try-catch block. It's a way to handle exceptions, which are errors that can happen while your program is running. You put the code that might cause an exception in the try block, and if an exception happens, the catch block will handle it. This helps keep your program from crashing and lets you nicely handle errors.

Live masterclass