0% found this document useful (0 votes)
12 views

Java Exceptions

Uploaded by

Kushal Singh 2
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Java Exceptions

Uploaded by

Kushal Singh 2
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Tutorial

Exceptions Handling

www.btechsmartclass.com
Introduction
In any programming there will be some problems like, trying to access a resource which is
not available, giving wrong input values, hardware problems like network connection etc.,
these kind of problems will not be known at the time of program compilation. The
compiler checks whether the program is as per the syntax rules of that programming
language….
These problems will came to know, when we try to execute(run) that program…. So we call
them as “Runtime Errors”
In such a kind of situation, What the Runtime machine does?
Simply, Stops program execution and causes abnormal termination.

This kind of situation is known as “EXCEPTION”


Definition
Dictionary Meaning: Exception is an abnormal condition

An exception is a problem that arises


during the execution of a program

An exception handling is the concept


which is used to handle exception
Concept
In java, Exception is an object that describes the exception
that occurred in a program.

Whenever exception occurs in a program, it creates an


object of respective exception class and sends it to a block
of code which handles that situation….. So that the
program execution will not be terminated abruptly.
Concept
Exception handling will not corrects the runtime error, but
just informs the user by providing some information
about that error.
Reasons
An exception can occur for many different reasons,
including the following….
A
A user
user has
has entered
entered invalid
invalid data
data

A
A file
file that
that needs
needs to
to be
be opened
opened cannot
cannot be
be found
found

AA network
network connection
connection has
has been
been lost
lost in
in the
the middle
middle of
of
communications
communications or
or the
the JVM
JVM has
has run
run out
out of
of memory
memory
Physical
Physical problem
problem like
like device
device not
not working,
working, cable
cable related
related problem
problem
Handling
Exceptions
To handle an exception in java we use the following
keywords…
11 try
try

22 catch
catch

33 finally
finally

44 throw
throw

55 throws
throws
Handling
Exceptions
To handle an exception in java we use the following
keywords…
11 try
try

22 catch
catch
The try keyword33
is used
finally
to define a
finally
block of statements which may
44 throw
throw
generate exception
55 throws
throws
Handling
Exceptions
To handle an exception in java we use the following
keywords…
22 catch
catch

11 to define
The catch keyword is used try
try a block of
statements which can handle the exception occurred
in try block 33 finally
finally
Every try block must have
44 atleast
throwone
throw catch block

The try block may have5multiple catch blocks


throws
5 throws
Handling
Exceptions
To handle an exception in java we use the following
keywords…
33 finally
finally

11 try
try
The finally keyword is used to a
block of statements
22 which must be
catch
catch
execute irrespective
44 throwof
throw Exception
occurance.
5 throws
5 throws
Handling
Exceptions
To handle an exception in java we use the following
keywords…
44 throw
throw

11 try
try

The throw keyword


22 catch is used to
catch

throw an exception
33 finally
finallyexplicitly.
55 throws
throws
throw
Keyword
import java.io.*;
Example class ThrowDemo{
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println(“Enter any two Integer values”);
int a = input.nextInt();
int b = input.nextInt();
try{
if(b == 0)
throw new Exception(“Can not divide!!!”);
int result = a / b;
System.out.println(“Result of a / b = ”+result);
}
catch(Exception e){
System.out.println(e);
}
}
}
Handling
Exceptions
To handle an exception in java we use the following
keywords…
55 throws
throws

11 try
try
The throws keyword
22 catch
is used to list
catch
the types of Exceptions that a
33 finally
finally
method might throw.
44 throw
throw
throws
Keyword
class ThrowsDemo {
void myMethod(int n) throws IOException,ClassNotFoundException {
Example if(n ==1)
throw new IOException(“Message 1 !!!”);
else
throw new ClassNotFoundException(“Message 2 !!!”);
}
}
class ThrowsDemoTest {
public static void main(String args[]) {
ThrowsDemo obj = new ThrowsDemo();
try{
obj.myMethod(1);
}
catch(Exception e){
System.out.println(e);
}
}
}
Categories
In java there are TWO types of exceptions

Checked
Checked Exceptions
Exceptions
The checked exceptions are checked at compile-time

Unchecked
Unchecked Exceptions
Exceptions
The unchecked exceptions are checked at runtime
Checked
Exceptions
Checked exceptions 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.

IOException
SQLException
DataAccessException
ClassNotFoundException
Checked
Exceptions
Example
import java.io.*;
class Example {
public static void main(String args[]) {
FileInputStream fis = new FileInputStream("B:/myfile.txt");
/*This constructor FileInputStream(File filename) throws FileNotFoundException */
int k;
while(( k = fis.read() ) != -1) {
System.out.print((char)k);
}
fis.close();
/*The method close() closes the file input stream * It throws IOException*/
}
}
Unchecked
Exceptions
Unchecked Exceptions mostly arise due to programming
errors like accessing method of a null object, accessing
element outside an array bonding or invoking method with
illegal arguments etc,.

NullPointerException
ArrayIndexOutOfBound
IllegalArgumentException
IllegalStateException
Exception
Hierarchy
In java all the Exceptions are defined as Classes to handle
them. All those classes are in following hierarchy…
Throwable

Error Exception

RunTimeException IOException SQLException

NullPointerException
IndexOtOfBoundsException
NumberFormatException
…..
Exception
Hierarchy
Exception
Hierarchy
Creating
Exceptions
To create your own exception types to handle situations just define a
subclass of Exception
class MyOwnException extends Exception {
public MyOwnException(String msg){
super(msg);
}
}
class EmployeeTest {
static void employeeAge(int age) throws MyOwnException{
if(age < 0)
throw new MyOwnException("Age can't be less than zero");
else
System.out.println("Input is valid!!");
}
public static void main(String[] args) {
try { employeeAge(-2); }
catch (MyOwnException e){ e.printStackTrace(); }
}
}

You might also like