Exception in Java

Introduction:

Exception: An exception is an unwanted or unexpected event, that disturbs the normal flow of the program i.e at run time is called an exception.

 

It is highly recommended to handle exceptions so that the program terminates normally. The main objective of exception handling is graceful (normal) termination of the program.

 

What is the meaning of exception handling?

Exception handling doesn’t mean we are repairing an exception. We have to define an alternative way to continue the rest of the program normally without any disturbance. This way of defining alternative is nothing but exception handling.

 

Example: Suppose our programming requirement is to read data from a remote file locating in India. At runtime, if the India file is not available then our program should not be terminated abnormally.

 

We have to provide a default local file to continue the rest of the program normally when the file is not found at runtime. This way of defining alternative is nothing but exception handling.

Example:

statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
try
{
read data from London file
}
catch(FileNotFoundException e)
{
use a local file and continue the rest of the program normally
}
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;

Suppose there are 10 statements in your program and there occurs an exception at statement 5 like FileNotFoundException, then the rest of the code will not be executed and the program terminated abnormally. If we provide a default local file to perform exception handling at statement 5, then the rest of the statement will be executed. That is why we use exception handling in Java.

Default exception handling in java:

  1. If an exception raised inside any method then that method is responsible to create an Exception object with the following information. Name of the exception.
  2. After creating that Exception object, the method handovers that object to the JVM.
  3. JVM checks whether the method contains any exception handling code or not. If the method won’t contain any handling code then JVM terminates that method abnormally and removes the corresponding entry from the stack.
  4. JVM identifies the caller method and checks whether the caller method contains any handling code or not. If the caller method also does not contain handling code then JVM terminates that caller method also abnormally and removes the corresponding entry from the stack.
  5. This process will be continued until main() method and if the main() method also doesn’t contain any exception handling code then JVM terminates main() method also and removes the corresponding entry from the stack.
  6. Then JVM handovers the responsibility of exception handling to the default exception handler.
  7. Default exception handler just prints exception information to the console in the following format and terminates the program abnormally.

 

Hierarchy of Java Exception classes:

The Throwable class is the root class of Java Exception hierarchy, which is inherited by two subclasses: Exception and Error. Throwable class present in java.lang package.

 

Different Between Exception and Error:

Exception:

Most of the case exceptions are caused by our program and these are recoverable by the programmer.

Ex: If FileNotFoundException occurs then we can use local file and we can continue rest of the program execution normally.

 

Error:

Most of the cases errors are not caused by our program these are due to lack of system resources and these are non-recoverable by programmers.

Ex: If OutOfMemoryError occurs being a programmer we can’t do anything the program will be terminated abnormally. System Admin or Server Admin is responsible to raise/increase heap memory.

 

Types of Java Exceptions:

There are mainly two types of exceptions: checked and unchecked.

  1. Checked Exception
  2. Unchecked Exception

So if we are talking about an error then an error is considered as the unchecked exception.

Checked Vs Unchecked Exceptions:

Checked Exception:

The exceptions which are checked by the compiler whether programmer handling or not, for smooth execution of the program are called checked exceptions. Checked exceptions are checked at compile-time but it is always occurring at runtime.

Example:
IOException, SQLException, etc.

 

Unchecked Exceptions:

The exceptions which are not checked by the compiler whether programmer handing or not, are called unchecked exceptions.  Unchecked exceptions are not checked at compile-time, but they are checked at runtime.

Example:
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.

 

Fully checked Vs Partially checked :

Fully checked:

A checked exception is said to be fully checked if and only if all its child classes are also checked.

Example:
1) IOException
2) InterruptedException

 

Partially checked:

A checked exception is said to be partially checked if and only if some of its child classes are unchecked.

Example:
Exception.

NOTES:-

  1. RuntimeException and its child classes, Error, and its child classes are unchecked and all the remaining are considered as checked exceptions.
  2. Whether exception is checked or unchecked compulsory it should occur at runtime only and there is no chance of occurring any exception at compile time.
  3. The only possible partially checked exceptions in java are  Throwable and Exception. 

 

Read More topics related to the collection.

 

Read more topics related to java

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

12 Replies to “Exception in Java”

  1. I intended to create you one little note just to give thanks as before for the fantastic information you’ve discussed at this time. It has been certainly particularly generous of people like you to allow extensively all that a lot of people could possibly have distributed as an e-book to earn some cash for their own end, especially considering the fact that you could have done it if you considered necessary. The basics additionally acted as a easy way to be aware that many people have a similar desire just as mine to figure out a great deal more concerning this condition. Certainly there are a lot more enjoyable instances ahead for folks who check out your blog.

  2. My husband and i ended up being cheerful Albert could carry out his analysis because of the ideas he acquired from your blog. It’s not at all simplistic just to always be freely giving facts a number of people could have been making money from. And now we acknowledge we’ve got the blog owner to appreciate because of that. All of the explanations you’ve made, the easy blog menu, the friendships you aid to create – it’s all astonishing, and it’s aiding our son and our family believe that that article is amusing, and that’s especially essential. Many thanks for all!

  3. I enjoy you because of all of the effort on this blog. Betty delights in carrying out internet research and it’s easy to see why. A lot of people hear all about the compelling form you convey advantageous guidance by means of the blog and attract contribution from other individuals about this area and my child has always been becoming educated a great deal. Enjoy the remaining portion of the new year. You’re carrying out a brilliant job.

  4. I precisely desired to say thanks again. I’m not certain the things I could possibly have achieved in the absence of the aspects contributed by you about my area. Completely was a challenging case for me, but being able to view a expert approach you dealt with it forced me to cry for contentment. Extremely grateful for the advice and believe you are aware of a powerful job that you’re accomplishing educating many others all through a blog. I’m certain you’ve never come across all of us.

  5. Thanks for your entire labor on this web site. My mother take interest in participating in research and it is simple to grasp why. My spouse and i notice all regarding the powerful tactic you produce useful thoughts via your website and in addition recommend response from the others on this subject then our own princess has been becoming educated a great deal. Enjoy the remaining portion of the new year. You’re carrying out a fabulous job.

  6. My husband and i ended up being cheerful Albert could carry out his analysis because of the ideas he acquired from your blog. It’s not at all simplistic just to always be freely giving concepts a number of people could have been making money from. And now we acknowledge we’ve got the blog owner to appreciate because of that. All of the explanations you’ve made, the easy blog menu, the friendships you aid to create – it’s all amazing, and it’s aiding our son and our family believe that that article is amusing, and that’s especially essential. Many thanks for all!

  7. I enjoy you because of all of the effort on this blog. Betty delights in carrying out internet research and it’s easy to see why. A lot of people hear all about the compelling form you convey advantageous guidance by means of the blog and attract contribution from other individuals about this area and my child has always been becoming educated a great deal. Enjoy the remaining portion of the new year. You’re carrying out a brilliant job.

Leave a Reply

Your email address will not be published.