Java Try-Catch Block
Java Try Block
The handling of the exceptions in a block of code is done with the help of java try block. It throws the code that is enclosed in a block that it means that throws the exception. This try block must and should be used in methods only.
The specific statements or code in a try block has been excepted when the remaining block of code is not executed. So, it is to be remembered that the code should not be kept in the try block so that it should no longer be an exception.
Syntax of try-catch in java language
try {
//block of program that handles the exception
}catch (class_Name_ofException ref) {}
Syntax of try-finally block in java language
try {
//block of program handles the exception
} finally block {}
Java Catch Block
The handling of the exception of the catch block in java programming language is done by verifying the type of exception depending on the given parameters. The exception which has been verified or initialize must be the parent class exception or generated exception type. Anyhow, declaring the generated exception type is a good approach.
Java try-catch: internal working
- Firstly, java virtual machine (JVM) will verify or check whether the given exception is handled or not.
- JVM gives a default value or default exception if exception is not handled.
- It also provides the default exception handler that works on the following tasks:
- Reprinting the exception description.
- Execute the stack trace (method of hierarchy where the exception occurs)
- Helps in terminating the program
After the exception there may be the presence of many lines of code that maybe 100 lines or more than it. Every code below the exception cannot be executed if the code is not handled.
Example 1
CatchEx1.java
public class CatchEx1 {
public static void main (String [] s) {
try
{
int i=80/0.
}
catch (e)
{
System.out.println(e).
}
System.out.println("remaining of the code prints");
}
}
Output:
remaining of the code prints
As Explained and shown from the above program, the remaining of the program is executed, i.e., the remaining of the code statement is printed.
Example 2
The program has been returned in a block that will not throw an exception.
CatchEx2.java
public class CatchEx2{
public static void main (String [] args) {
try
{
int d=110/0.
System.out.println("remaining of the code");
}
catch (ArithmeticException e)
{
System.out.println(e);
}
}
}
Output:
/ by zero
An exception occurs in the try block of the given code or program when the rest of the block code will not be printed or run.
Example 3
we will be using the parent class exception to handle the exception in this example.
CatchEx3.java
public class CatchEx3 {
public static void main (String [] s) {
try
{
int A=150/0.
catch (Exception e)
{
System.out.println(e);
}
System.out.println("remaining of the program is printed").
}
}
Output:
remaining of the program is printed
Example 4
Here we have an example to modify the exception in a block of catch.
CatchEx4.java
public class CatchEx4 {
public static void main (String [] s) {
int k=400;
int l=0;
int data;
try
{
data=k/l.
}
catch (Exception e)
{
// the exception in catch blockis modifying or resolving
System.out.println(k/l+2)).
}
}
}
Output:
200
Example 5
From this example, consideration with try block, we also show exception code in a catch block.
CatchEx5.java
public class CatchEx5 {
public static void main (String [] s) {
try
{
int A1=500/0.
}
// exception is handled
catch (Exception e)
{
// generating the exception in catch block
int A2=500/0; //may throw exception
}
System.out.println("remaining of the code");
}
}
In this example the exception code is not written in the catch block. So, the code which is in a try block and catch block can only handle the exceptions of that program.
Example 6
we maintain the given exception (Arithmetic Exception) with various types of exception class (ArrayIndexOutOfBoundsException-AIOOBException).
CatchEx6.java
public class CatchEx6 {
public static void main (String [] args) {
try
{
int B=50/0.
}
catch (AIOOBException e)
{
System.out.println(e);
}
System.out.println("remaining of the code");
}
}
Example 7
An example to handle checked exception.
TryCatchEx7.java
import java.io. FileNotFoundException.
import java.io. PrintWriter.
public class TryCatchEx7{
public static void main (String [] args) {
pw.
try {
pw = new PrintWriter("jtp.txt").
pw. println("saved").
}
// checked exception handler by providing handling access
catch (FileNotFoundException e) {
System.out.println(e).
}
System.out.println("successfully file is saved").
}
}
Output:
Successfully file is saved