Checked and Unchecked Exceptions in Java

In the previous tutorial, we learned about the String class methods in Java. In this tutorial, we will learn about Checked and Unchecked Exceptions in Java.

210

Exceptions in Java are divided into the two categories – Unchecked exceptions & Check exceptions. In this article we will learn how these works with syntax and examples.

Unchecked Exceptions in Java

In the hierarchy, all the descendant classes of the Runtime Exception come under this category.

Checked Exceptions in Java

The sub-classes of all other classes come under this Category.

Checked and Unchecked exceptions in java
Checked and Unchecked exceptions in java

As we have learned that there are four possibilities to throw and catch for an exception –

  • Default Throw and default catch
  • Default Throw and our catch
  • Our Throw and default catch
  • Our Throw and our catch

Below, we will understand the Default Throw and our catch –

Default Throw and our catch:

To implement this, we have to use 3 keywords which are tried, catch, and finally.

try {
<code>
}
catch (<exception type> <parameter>) {
//0 or more <statements>
}
finally {
// finally block <statements>
}

In this, the statements where the exception is likely to occur are written inside the try block.

 And, when this exception occurs, the code to be run is written inside the catch keyword. The parameter inside the parenthesis, the name of the exception class, and its reference variable are made. This reference variable catches the object that is thrown when the exception occurs. And, finally, the block contains the code that will definitely run.

We will understand it better with the help of the example given below –

class Example{
public static void main(string[] args){
try{
System.out.println(3/0);
System.out.println(“In try”);}
}
catch(ArithmeticException e){
System.out.prinln(“Exception :” + e.getMessage());
}
System.out.println(“Hello”);
}

Output –

Exception : / by zero
Hello

Explanation –

In this code, as soon as the exception occurs in 3/0 statement, the code jumps to the catch block. And that’s why the line after that i.e, System.out.println(“In try”) will not run. And all the lines after the catch block will run after printing the error using the getMessage() method.

For every try block, it is necessary to write a catch block or finally block.

If more than one catch block is written, for more than one type of exception, all the catch blocks are matched and the best suitable with the type of exception is run. And, if no parameter of catch block matches, the default catch mechanism will run.

Finally is the block which will definitely run whether exceptions exist or not.

Some important points that we need to remember are given below –

  • For each try block, there can be zero or more catch blocks, but only one final block.
  • The catch blocks and finally block must always appear in conjunction with a try block
  • A try block must be followed by either at least one catch block or one final block.
  • The order exception handlers in the catch block must be from the most specific exception.

Apart from this, a program can explicitly throw an exception using the throw statement beside the implicit exception thrown.

Syntax –

  • Throw <throwableInstance>;

We will understand this better with the help of the example given below –

class Example{

public static void main(String[] args){

int balance = 5000;

int withdrawlAmount = 6000;

if(balance< withdrawlAmount)

throw ArithmeticException(“Insufficient balance”);

balance = balance -  withdrawlAmount;

System.out.println(“Transaction Successfully Completed”);

System.out.println(“Program Continue…”);

}}

Output –

Exception in thread “main” java.lang.ArithmeticException : Insufficient balance

So, this way we can also use it to set a user-defined syntax of error.

So, above we have learnt about the default throw and default catch mechanism.

Below, we will learn how to use our throw mechanism instead of default one.

A program can explicitly throw an exception using the throw statement besides the implicit exception thrown.

Syntax –

throw <throwable>;

This is used in case of situations that java doesn’t consider to be an exception. But is an exception according to the program that we have made.

Explicit Throw

Below is an example for Our Throw and Default Catch mechanism –

class exampleclass{

public static void main(String[] args){

int balance = 5000;

int withdrawlAmount = 3000;

if(balance<withdrawlAmount)

throw new ArithmeticException(“Insufficient balance”);

balance = balance – withdrawlAmount;

System.out.prinln(“Transaction Successfully completed”);

System.out.println(“Program continue…”);

}

}

In this, we have made the object of Class ArithmeticException and thrown it. Therefore, the error message “Insufficient balance” is printed.

Also, we can throw explicitly and write our catch mechanism –

class exampleclass{

public static void main(String[] args){

int balance = 5000;

int withdrawlAmount = 6000;

try{

if(balance<withdrawlAmount)

throw new ArithmeticException(“Insufficient balance”);

balance = balance – withdrawlAmount;

System.out.prinln(“Transaction Successfully completed”);}

Catch(ArithmeticException e){

System.out.prinln(“Exception : e.getMessage());

}

System.out.println(“Program continue…”);

}

}

Why should we throw an exception object?

  • Because we want to set a different message

Because java cannot recognize the exceptional situations of business logic

So, in this article, we have learned about the exception handling in Java in detail. Exceptional handling is very important for programming for business purposes and common situations.

Related Quiz:

Previous articleHow SSL Benefits SEO Strategy | SSL and SEO
Next articleList Methods in Python
Hello Guys, This website is maintained by the Yuvayana Writing Staff. We are trying to give you new and useful articles on daily basis. You can help us by sharing these articles with your friends on social networking websites. Please also provide your feedback via comment of each and every test that you have attempted.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.