Object Oriented Programming: Exception Handling in Java
Object Oriented Programming: Exception Handling in Java
Object Oriented Programming: Exception Handling in Java
Lecture 12
Exception Handling in Java
MCS
Exception Handling in Java
• The exception handling in java is one of the
powerful mechanism to handle the runtime errors
so that normal flow of the application can be
maintained
• What is exception?
– Dictionary Meaning: Exception is an abnormal
condition.
– In java, exception is an event that disrupts the normal
flow of the program. It is an object which is thrown at
runtime.
• What is exception handling?
– Exception Handling is a mechanism to handle runtime
errors such as ClassNotFound, IO, SQL, Remote etc.
Advantages of using Exception Handling
MCS
• The core advantage of exception handling is to maintain the normal flow
of the application. Exception normally disrupts the normal flow of the
application that is why we use exception handling. Let's take a scenario:
1. statement 1;
2. statement 2;
3. statement 3;
4. statement 4;
5. statement 5; //exception occurs
6. statement 6;
7. statement 7;
• Suppose there are 7 statements in your program and there occurs an
exception at statement 5, rest of the code will not be executed i.e.
statement 6 to 7 will not run.
• If we perform exception handling, rest of the code will be executed. That
is why we use exception handling in java.
MCS
What is an Exception?
• Exceptional event
• Error that occurs during runtime
• Cause normal program flow to be disrupted
• Examples
– Divide by zero errors
– Accessing the elements of an array beyond its range
– Hard disk crash
– Opening a non-existent file
– Heap memory exhausted
– A user has entered invalid data.
– A file that needs to be opened cannot be found.
– A network connection has been lost in the middle of communications or
the JVM has run out of memory.
MCS
Exceptions
• Error class
– Used by the Java run-time system to handle errors occurring in
the run-time environment
– Generally beyond the control of user programs
– Examples
• Out of memory errors
• Hard disk crash
• Exception class
– Conditions that user programs can reasonably deal with
– Usually the result of some flaws in the user program code
– Examples
• Division by zero error
• Array out-of-bounds error
MCS
Types of Exception
• There are mainly two types of exceptions: checked and unchecked where
error is considered as unchecked exception. The sun microsystem says there
are three types of exceptions:
1. Checked Exception
2. Unchecked Exception
3. Error
• Checked Exception
– The classes that extend Throwable class except RuntimeException and Error are
known as checked exceptions e.g.IOException, SQLException etc. Checked
exceptions are checked at compile-time.
• Unchecked Exception
– The classes that extend RuntimeException are known as unchecked exceptions e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.
Unchecked exceptions are not checked at compile-time rather they are checked at
runtime.
• Error
– Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
MCS
Checked Exceptions
• A checked exception is an exception that is typically a user error or a
problem that cannot be foreseen by the programmer.
• For example, if a file is to be opened, but the file cannot be found, an
exception occurs.
• These exceptions cannot simply be ignored at the time of compilation.
• It means if a method is throwing a checked exception then it should
handle the exception using try-catch block or it should declare the
exception using throws keyword, otherwise the program will give a
compilation error.
• Here are the few other checked exceptions
– SQLException
– IOException
– DataAccessException
– ClassNotFoundException
– InvocationTargetException
MCS
Unchecked Exceptions
• In most cases, unchecked exceptions reflect programming logic
errors that are not recoverable.
• For example,
– A NullPointerException is thrown if you access an object through a
reference variable before an object is assigned to it;
– An IndexOutOfBoundsException is thrown if you access an element
in an array outside the bounds of the array.
• These are the logic errors that should be corrected in the
program.
• Unchecked exceptions can occur anywhere in the program.
• To avoid cumbersome overuse of try-catch blocks, Java does
not mandate you to write code to catch unchecked exceptions.
• Note: All Unchecked exceptions are direct sub classes of
RuntimeException class.
MCS
Common Exception Scenarios
• Scenario where ArithmeticException occurs
– If we divide any number by zero, there occurs an
ArithmeticException.
int a = 50 / 0; // ArithmeticException