Exception Handling | A Beginners Guide:

Pranjal Goyal
CodeX
Published in
3 min readMar 31, 2022

As every fielder in cricket tries to catch a catch similarly a software developer should also catch an exception.

Exception handling is one of the most important and powerful tools to keep a normal execution of your beloved code.

Exception Handling

This article will focus on why exception handling is needed and what are some of the better approaches to handle an exception. The concept behind it remains the same in most programming languages just the syntax changes.

Introduction to Exception:

There are mainly 2 types of exceptions:

  • Checked Exception: This type of exception occurs at compile time. A bookish definition I know, let's see a real-world example.

Example: Let's say you are writing a code to open a file and in Java, it will not compile unless you provide a try-catch block around the code that is responsible for opening/reading that file.

This is mainly because the file that you are trying to fetch may not exist so it throws a compile-time exception FileNotFoundException which you have to handle.

  • Unchecked Exception: This type of exception occurs at run time. Hmm, example??

Example: Let's say you are writing have a code with takes two numbers and divides them, now it can be possible that the denominator is zero which can’t be checked at compile time. So we have to personally take care of such exceptions.

If it is not handled it will still run but this exception we need to take care of. The run time exception that will occur is ArithmeticException.

Handling Mechanism:

  • Try Block:

Code that is bound to raise an error if something goes wrong is put inside a try block. In simple words, the code which will not work in some situations is put inside a try.

  • Catch Block:

The catch block provides an alternate route for code to wrong when try throws an exception.

  • Finally Block:

This block will execute no matter if the code throws an exception or not.

Better Handling of Exceptions:

It is always better to specify an Exception that you are trying to catch. The main advantage of doing it is to send an appropriate response according to the type of exception rather than catching a generic Exception.

For example: Let us say there is a service that fetches a user from the database. If the user is not found it raises UserNotFoundException.
So it is better to catch (UserNotFoundException) rather than a general (Exception).

Secondly, if cases of multiple catches statements the exception which is definitive and specific should be kept first, since the order of catch statements matters. The generic Exception should be kept at last.

Till next time keep exploring keep learning…

“Programming isn’t about what you know; it’s about what you can figure out.” — Chris Pine

Connect with me:

Wanna talk about something let’s connect?

Linkedin | Twitter | Github

--

--

Pranjal Goyal
CodeX
Writer for

Coding is ❤ | Computer Graduate | Learning New Things Everyday… | I write good stuff