You are on page 1of 11

Unit 3

Exception Handling & I/O


An exception (or exceptional event) is a problem that
arises during the execution of a program. When
an Exception occurs the normal flow of the
program is disrupted and the program/Application
terminates abnormally, which is not recommended,
therefore, these exceptions are to be handled.
• An exception can occur for many different reasons.
Following are some scenarios where an exception
occurs.
• A user has entered an 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.
• Checked exceptions − A checked exception is an exception
that is checked (notified) by the compiler at compilation-time,
these are also called as compile time exceptions. These
exceptions cannot simply be ignored, the programmer should
take care of (handle) these exceptions.
• Unchecked exceptions − An unchecked exception is an
exception that occurs at the time of execution. These are also
called as Runtime Exceptions. These include programming
bugs, such as logic errors or improper use of an API. Runtime
exceptions are ignored at the time of compilation.
• Errors − These are not exceptions at all, but problems that
arise beyond the control of the user or the programmer. Errors
are typically ignored in your code because you can rarely do
anything about an error. For example, if a stack overflow
occurs, an error will arise. They are also ignored at the time of
compilation.
Exception Hierarchy
Exceptions Methods:
Exception Type Cause of Exception

ArithmeticException Caused by math errors such as division by zero

ArrayIndexOutOfBoundsException Caused by bad array indexes

ArrayStoreException Caused when a program tries to store the wrong type of data in an array

FileNotFoundException Caused by an attempt to access a nonexistent file

IOException Caused by general I/O failure, such as inability to read from file

NullPointerException Caused by referencing a null object

NumberFormatException Caused when a conversion between strings and number fails

OutOfMemoryException Caused when there’s not enough memory to allocate a new object

SecurityException Caused when an applet tries to perform an action not allowed by


browser’s security setting

StackOverflowException Caused when the system runs out of stack space

StringIndexOutOfBoundsException Caused when a program attempts to access a nonexistent character


position in a string
• Catching Exceptions: A method catches an exception
using a combination of the try and catch keywords.
A try/catch block is placed around the code that
might generate an exception. Code within a try/catch
block is referred to as protected code, and the syntax:

try { // Protected code }


catch (ExceptionName e1) { // Catch block }

The code which is prone to exceptions is placed in the


try block. When an exception occurs, that exception
occurred is handled by catch block associated with it.
Every try block should be immediately followed
either by a catch block or finally block.
Multiple Catch Statements
It is possible to have more than one catch statements in the catch block.
 
try
{
 
 
}
catch (Exception-type1 e)
{
 
}
catch (Exception-type2 e)
{
 
}
.
.
catch (Exception-typeN e)
{
 
}
………………………
………………………
• Using finally statement
Java supports another statement known
as finally statement that can be used to
handle an exception that is not caught by
any of the previous catch statements.
finally block can be used to handle any
exception generated within a try block. It
may be added immediately after the try
block or after the last catch block.
try try
{{
……… ……..
}}
finally catch (……..)
{{
 
}}
………
finally
{
 
}
• When finally block is defined, this is guaranteed to execute,
regardless of whether or not an exception thrown. As a result, we can
use it to perform certain house-keeping operations such as closing
files and releasing system resources.
Throwing out own Exceptions
There may be times when we would like to throw our own exceptions. We can do this by using the
keyword throw .
throw new Throwable_subclass ;
Example:
throw new ArithmeticException;
Example of user defined exception:
class MyExc extends Exception{
MyExc (String msg)
{
super(msg);
}
}
class MyException{
public static void main(String args[])
{
try
{
throw new MyExc("Hello");
}
catch (MyExc e)
{
System.out.println("Caught MyExc");
System.out.println(e.getMessage());
}
}
}

You might also like