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 CodeInternal 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 CodeOutput:
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 CodeOutput:
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.