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

Lecture-27 Java SE (Exception Handling-1)

Uploaded by

godas91682
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lecture-27 Java SE (Exception Handling-1)

Uploaded by

godas91682
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Java SE(Core java)

Lecture-27
Today’s Agenda
01 Exception Handling

02 Keywords used in Exception Handling

03 Keywords try and catch

04 Exception hierarchy
Exception Handling
• Exception in programming languages like java means run time errors i.e. errors
which appear during execution of a program.

• It might be due to user’s wrong input or any logical fallacy of the program.

• Exception handling is the behavior of a program after an exception occurs.

• But before understanding how to handle exception, first let us understand what java
does when an exception occurs.

• By default java takes 2 actions whenever an exception occurs -


Exception Handling
How java handles it???
1. It immediately kills the program on the line where the exception occurs.

2. It defines the reason for exception but is highly technical and is not friendly to an
user.

Both the above actions are not user friendly because,


• If exception occurs at least those lines should continue to run which are not related
with the exception.

• It would be much better if our program displays an easy to understand message


regarding the exception so that the user can become aware about his mistakes.
Exception handling Keywords
• Java provides us keywords which can be used to write handle exceptions in
programmers own way, which will be much more user friendly –
1. try

2. catch

3. throw

4. throws

5. finally
try and catch
• Syntax:-
try Those lines on which an exception may occur are written
{ in the try block.
--- As soon as an exception occurs, java leaves the try
--- block and moves towards the catch block.
}
catch(<Exception class name> <object reference>)
{ Object reference points to that object which is sent
--- by the try block after an exception occurs.
--- The class should be similar to the exception which
} has occurred. Example, ArithmeticException
class.
try and catch
• There cannot be any other line between a try and a catch block, they should be
continuous.

• A try block can have multiple catch blocks.

• All exceptions are pre defined classes in java. If no catch block matches the exception
object then java shows its default behavior.

• What is to be specified in the catch block is completely on the programmer.

• Let us understand this through an example.


Example
import java.util.*;
class DivideAndSum
{
public static void main(String args[])
{
Scanner kb=new Scanner(System.in);
System.out.println("Enter two int");
int a=kb.nextInt( );
int b=kb.nextInt( );
Example
try
{
int c=a/b;
System.out.println("Division= "+c);
}
catch(ArithmeticException ex)
{
System.out.println(“Please input non zero denominator");
}
int d=a+b;
System.out.println("Sum is="+d);
}
}
Exception Hierarchy
Throwable

Error Exception

It represents those exceptions This class represents those exception


which are not meant to be which can and should be handled by
handled by programmers. a programmer in his program.
They are either handled by JVM All exception classes are derived
or OS. class of Exception class.
Exception Hierarchy
Exception
RuntimeException
ArithmeticException SQLException
IOException
NoSuchElementException
FileNotFoundException
InputMismatchException
EOFException
NumberFormatException
MalformedURLException
IndexOutOfBoundsException
ArrayIndexOutOfBoundsException
SocketException
StringIndexOutOfBoundsException
NullPointerException
NullPointerException
Java’s rule on multiple catch
• Java has a very strict rule while using multiple catch for a try block.

• They rule is that, a parent class of exception hierarchy cannot come before its
child class.

• This is because a reference of parent class can easily point to the child class
object and hence, the child class catch block will never run.
Example
try try
{ {
---- ----
---- ----
} }
catch(IOException e) catch(FileNotFoundException f)
{ {
---- ----
---- ----
} }
catch(FileNotFoundException f) catch(IOException e)
{ {
---- ----
---- ----
} }
Exercise
• WAP to accept 2 integers from the user and display the result of their division and
sum. Your program should behave in the following way –
1. If both the inputs are integers and are valid then the program should display the
result of their division and sum.
2. If denominator is then program should display relevant error message but should
display the sum.
3. If input value is not an integer then the program should display relevant message
and neither division nor sum should be displayed.
Solution
import java.util.Scanner;
class DivideAndSum
{
public static void main(String [ ] args)
{
Scanner kb=new Scanner(System.in);
int a=0,b=0;
try
{
System.out.println(“Enter two numbers”);
a=kb.nextInt();
b=kb.nextInt();
int c=a/b;
System.out.println(“Division is ”+c);
}
Exercise
catch(ArithmeticException e)
{
System.out.println(“Denominator should not be 0”);
}
catch(InputMismatchException ex)
{
System.out.println(“Please enter integers only”);
System.exit(0);
}
int d=a+b;
System.out.println(“Sum is ”+d);
}
}
Thank you

You might also like