Exception Handling In Java

Sharing is caring!

Hello, Guys In this article I will try to explain you the Exception Handling In Java

Before starting I want to ask you a question.

If you are a startup programmer and if you just starting to learn a new programming language so, if you try to implement any new logic you will do something you will write up code then will you compile it

Now, my question is after compiling your first program which thing you want to see first the Output or the Error of that program? 

The answer is you don’t have to concern on the Output of that program you should concern on the Error of that program.

Now, it’s time to explain our topic.

If we writing up a program on JAVA we will see some errors in our program but in JAVA we have different types of error.

Exception handling in java

Types Of Error’s in Java

So, if we talk about errors in JAVA we have two things:

  1. Error’s
  2. Exceptions

But we know in this article we try to get some knowledge about Exception Handling In Java so, we are taking Exceptions that time.

Exception Handling In Java

What Is Exceptions?

For understanding Exceptions, we have to see some examples.

Example#1

Let’s suppose you are trying to open a file and file is not exist right so, there is an exception.

Example#2

If you trying to run a process but CPU is not available so, this is an exception.

Example#3

If you try to divide a number by zero so, that’s an exception.

Exception Handling In Java

Difference Between Exceptions and Error’s

The main difference between exceptions and errors are:

Exceptions:

We can handle Exceptions as a user or as a programmer

Error’s

We can’t handle errors.

For Example:

  • Your JVM is not working Properly
  • Your RAM is going out of Memory.

So, what you can do for that nothing.

Now, again we have to types of Exceptions

  1. Checked Exception
  2. Un-Checked Exception

In another article, we will talk about types of exception.

Exception Handling In Java

So, how to do Exception Handling In Java, first thing whenever you work with exception handling you should focus on statements.

Hey, whenever you write Java code you write multiple statements. Now in this statement, there are two types of statements.

  1. Normal Statement
  2. Critical Statement

So, for understanding Exception Handling In Java, we can take an example of code.

Here we can write some codes:

 

PSVM ( )

{

      int i = 5;

      int j = 2;

      int k = i/j;

      SOP (k);

}

 

So, the code looks so simple you have 4 statements above and when you execute this type of statements it will give you some output and output is 2 because when you say 5 / 2 it will give you 2 as your quotient and 1 as your remainder.

 

But the problem is, there are two different types of statements are a presence in above 4 statements Simple and Critical statements.

Now the question arises how we can find which statement Normal statement and which statement Critical Statement.

 

Let’s see whenever you execute the statement No. 1 you will see this statement will give you output this statement will work. And whenever you will execute statement No.2 it also works because j=2 shown simple and the 3rd statement is int k = i/j which looks normal. But, the problem is In case your j value is 0 (zero) you will do i/j it means 5/0 it should be infinity but in a programming language, it will give you an error.

 

So, you have to handle this error

What programming standards say to handle this error:

Programming standard says you have to write your normal statements normally

We can write as:

PSVM ( )

{

   int i = 5;

   int j = 2;

And here we know that the third statement is critical as we discussed above so we have to enclose the third statement in a Try block like

    {

        k= i/j;

     }

So, if this Try block gives you an error, then you have to Catch Exception.

    Catch (Exception e) { }

 

So, what normally you have is whenever you work with any critical statement that critical statement should be in a Try Block.

SOP (k);

}

If the statement which was in the try block its execute normally then it will continue for SOP will skip this Catch Block but if your critical statement generates or closed an error so in that case, you have to catch.

So, your Try will Throw and your Catch will Catch and it will catch the Exception.

Which looks like below:

PSVM ( )

{

      int i = 5;

      int j = 2;

        {

           k= i/j;

        }

       Catch (Exception e) { }

       SOP (k);

}

So, guys this is how you can handle the Exceptions in JAVA.

Hey if you like this article then you should also learn this one 

“Top 10 Programming Languages To Keep You Employed

 

Sharing is caring!

Leave a Reply

avatar
  Subscribe  
Notify of