All about Exception and Exception Handling

Sakshi Saxena
funccFORCE
Published in
3 min readMar 27, 2021

During program development, there may be some cases where the programmer does not have the certainty that this code fragment is going to work right, either because it accesses to resources that do not exist or because it gets out of an unexpected range. These types of contradictory or unexpected situation during program execution is known as exception and the way to handle them is called exception handling.

It can be generated by an error in the JVM, an error in your program or explicitly via a throw statement.

1. Compile time error — These are the errors resulting out of violation of programming languages grammar rules example writing SIM tactically in correct statement will result into compile type error.

2. Runtime errors — The errors that occur during runtime because of unexpected situations.

For example,

class DivbyZero 
{
public static void main ( String args [ ] )
{
System.out.println(3/0);
System.out.println("Print the answer");
}
}

If you execute the above code you will receive a message -

Exception in thread “main”
java.lang.ArithmaticException: /by zero
at DivbyZero.main (DivbyZero.java:3)

It is generated by default exception handler of Java which prints out the exception description, stack trace and causes the program to terminate.

A few terminology used with an exception handling are -:

1. Throwing — Process by which an exception is generated and passed to the program.

2. Catching — Capturing an exception that has just occurred and executing statements that try to resolve the problem.

3. Catch clause — The block of code that deals with the exception.

4. Stack trace — The sequence of method calls that brought control to the point where the exception occurred.

In Java exceptions are treated as objects and if the handling has not been done the program will terminate with a display of the exception class. Some of the common exception classes are -

1. IOException — error while handling IO.

2. NumberFormatException — error when invalid format of number is encountered.

3. Arithmetic Exception — Throws if a program attempts to perform division by zero.

4. StringIndexOutOfBoundsException — Throws if a program attempts to access a character at a non existent index in a string.

Using try and catch block -

To intercept and control an exception you use a try/catch/finally construction.

The lines of code that are part of the normal processing sequence are in a try block.

The code to deal with the exception that might arise during execution of the try block is placed in a catch block.

Finally keyword can be used to provide a block of code that is always executed regardless of whether an exception is signalled or not.

For example,

try {
file = new DataInputStream( new InputStream ( fileName ));
}
catch ( Exception e )
{
System.out.println ( "\n Exception occured" );
}
finally
{
//always executed
}

Can an Exception be forced?

The throw keyword is used to force an exception. That means a programmer can force an exception to occur through throw keyword.

The throws keyword is used to tell the compiler that we haven’t handled the exception, so the exception will be handled by the calling function.

For example,

throw new FileNotFoundException ( "Could not find abc.txt" );

Remember -

1. For each try block there can be zero or more catch blocks but only one finally block.

2. The catch block and the finally block must always appear in conjunction with a try block.

3. The try block must be followed by either at least one catch block or one finally block.

4. Code in a finally clause is executed whether or not an exception is caught.

I hope this blog will provide you with sufficient information. If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.

--

--

Sakshi Saxena
funccFORCE

SWE @UBS | Ex - OnePlus, American Express | vGHC'21Mentor @CircuitVerse | Postman Student Expert | GDSC BV Team Member