Sei sulla pagina 1di 7

Java Exceptions

EXCEPTION AND EXCEPTION HANDLING DEFINITIONS:


Contradictory or unexpected errors that occur during runtime of a
program are known as exceptions.
The method of handling these runtime anomalies (runtime errors) in a
program is known as exception handling.

ADVANTAGES:
-

It separates error-handling code from normal code


Makes the code clear (clarifies the code)
Enhances readability
Ensures clear, robust, fault-tolerant programming
Ensures that the program does not terminate abnormally.

WHEN TO USE EXCEPTION HANDLING:


- While processing situations prone to errors
- When widely used components such as libraries, classes, functions, etc are
involved in the code
- In large projects that require error-processing.

COMMON SCENARIOS WHERE EXCEPTIONS MAY


OCCUR:
There are given some scenarios where unchecked exceptions can occur. They
are as follows:
1) Scenario where ArithmeticException occurs
If we divide any number by zero, there occurs an ArithmeticException.
Example:
int a=50/0;//ArithmeticException

Page 1 of 7

2) Scenario where NullPointerException occurs


If we have null value in any variable, performing any operation by the variable
occurs an NullPointerException. Example:
String s=null;
System.out.println(s.length());//NullPointerException

3) Scenario where NumberFormatException occurs


The wrong formatting of any value may occur NumberFormatException.
Suppose I have a string variable that have characters, converting this variable
into digit will occur NumberFormatException. Example:
String s="abc";
int i=Integer.parseInt(s);//NumberFormatException

4) Scenario where ArrayIndexOutOfBoundsException occurs


If you are inserting any value in the wrong index, it would result
ArrayIndexOutOfBoundsException as shown below:
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException

HIERARCHY OF EXCEPTIONS:
Page 2 of 7

All exception classes are subtypes of the java.lang.Exception class. The


exception class is a subclass of the Throwable class. Other than the
exception class there is another subclass called Error which is derived
from the Throwable class.

HOW TO USE EXCEPTION HANDLING:


To intercept and thereby control an exception we must use the
Page 3 of 7

try/catch/finally constructor sequence.


TRY BLOCK:
- Contains that part of code that is prone to runtime errors.
- It is thus an error trapper.
- Syntax:
try
{
Statement(s).
}

CATCH BLOCK:
- Contains the code that deals with the probable exception(s) that
may arise during run-time
- If there are multiple exceptions that may arise in the try block
during run time then several catch blocks must be made to handle
them.
- Note: This block is executed only if an error is detected, ie, an
error is caught .
- Syntax:
catch(Exception e)// here exception means any possible exception
//e refers to any literal
{
Statement(s).
}

- While multiple catch blocks can be used, there is a certain rule to


follow to make use of exception handling effectively and
efficiently. This rule is that while using multiple catch blocks, in
order to obtain efficient code, we must follow the hierarchy of
exceptions (refer to above).
- Catches for specific exceptions must always be written prior to
the generic handler. Example: IOException catch will occur
before the generic Exception catch.
FINALLY BLOCK:
Page 4 of 7

- Contains the code that must be executed no matter what. It is


always performed regardless of whether an exception is signaled or
not.
- Note: It is the only block that is not compulsory to use in exception
handling.
- Syntax:
finally
{
Statement(s)
}

THE THROWS KEYWORD:


- The throws keyword is used to inform that an error has occurred by
throwing an exception out of a method.
- It is mostly specified with the method (function) prototype.
Example:
Public void main() throws IOException

- Note: one must not confuse the throw keyword with the
throws keyword. The throw keyword is used to throw an
exception manually, ie, to force an exception. Thus it is very
different to the throws keyword.

AN EXAMPLE FOR EFFICIENT EXCEPTION


HANDLING (USING SYNTAX AND CATCH RULES):
Page 5 of 7

try
{
Statement(s)
}
catch (FileNotFoundException e1) // this exception is categorized under
IOExceptions
{
Statement(s)
}
catch(IOException e2)
{
Statement(s)
}
catch(Exception e3)
{
Statement(s)
}
finally // not compulsory.
{
Statement(s)
}

Sources:
Links:
http://www.javatpoint.com/exception-handling-in-java
http://www.tutorialspoint.com/java/java_exceptions.htm
http://beginnersbook.com/2013/04/java-exception-handling/
https://docs.oracle.com/javase/tutorial/essential/exceptions/
books:
COMPUTER APPLICATIONS by Sumita Arora
TOGETHER WITH COMPUTER APPLICATIONS
UNDERSTANDING COMPUTER APPLICATIONS WITH BLUEJ by Vijay Kumar and
Dilip Kumar Dey (Arya publishing company)

----------------------------------------------------------------------------------------------

Page 6 of 7

Page 7 of 7

Potrebbero piacerti anche