SlideShare a Scribd company logo
9
Most read
16
Most read
19
Most read
Exception Handling
In JAVA
-M Vishnuvardhan,
Dept. of Computer Science,
SSBN Degree College, ATP
SSBN Degree College, ATP M Vishnuvardhan
Introduction
In general a program contains the
following types of errors
a)Compile-Time Errors
b)Run-Time Errors
SSBN Degree College, ATP M Vishnuvardhan
Compile-Time Errors
Compile time errors are mainly due to the following reasons
a)Misspelt of the keywords i.e., float x,y;
b)Wrong Identifier names i.e., int avg?marks;
c)Wrong Syntax i.e., missing of semicolon …
d)Wrong semantics i.e., int x=3.14;
These errors are identified by the compiler and can be rectified very
easily
SSBN Degree College, ATP M Vishnuvardhan
Run-Time Errors
In programs there are certain types of errors which are not
found at compilation time. These are known at the time of
running. The compiler cant find these category of errors.
Run-time errors are mainly due to the wrong input and wrong
logic in the program. Run-time errors are called as Exceptions.
E.g., int a,b,c;
a= 20; b=0;
c= a/b;
SSBN Degree College, ATP M Vishnuvardhan
Exception - Defined
An desirable/ abnormal situation which prevail inside the
program is called as an Exception.
In general when ever exception occurs the program suddenly
stops. This leads to a major problem when working with files,
databases, network connections….
Exception handling is a technique or mechanism of handling
seriousness of the exception.
Traditional programming languages lack of exception handling
mechanism. But all modern programming languages are capable
of handling the exception
Robust programming language
SSBN Degree College, ATP M Vishnuvardhan
Exceptions in java
The undesirable condition in java are categorized in to two types
1.Error – Hardware related can’t be handled from the program
2.Exception – Software related can be handled from the program
Object
Throwable
Error Exception
StackOver
FlowError
Virtual
MachineError
Checked
Exception
Unchecked
Exception
UserDefined
Exception
SSBN Degree College, ATP M Vishnuvardhan
Categories of Exceptions
Exception is the base class of all types of the Exceptions
Checked Exception:- Must be handled by the programmer other wise
compile time error is generated
Unchecked Exception:- It may or may not be handled by the programmer
no compile time error is generated. RunTimeException is the base class of
all unchecked Exceptions
User Defined Exceptions:- In general java deals with common types of
exception for a specific type of exception. The programmer has to define
the exceptions of the specific type those are called as User-defined
Exceptions
SSBN Degree College, ATP M Vishnuvardhan
Checked Exceptions
Exception
FileNotFoundException
IllegalThreadStateException
InterruptedException
IOException
SQLException
SSBN Degree College, ATP M Vishnuvardhan
Checked Exceptions
FileNotFoundException:
Occurs when an attempt is made to open a file which is not
actually present.
IllegalThreadStateException:
Occurs when a thread is moved to an illegal state
InterruptedException:
Occurs when a Thread is interrupted.
IOException:
Occurs due to the failure of the h/w or s/w associated with
the I/O devices etc.,
SQLException:
Occurs when a database connection is not available or when it
returns null.
SSBN Degree College, ATP M Vishnuvardhan
RunTimeException
RunTimeException
ArithmeticException
NumberFormatException
NullPointerException
IndexOutOfBoundsException
ArrayIndexOutOfBoundsException
StringIndexOutOfBoundsException
SSBN Degree College, ATP M Vishnuvardhan
Runtime Exceptions
ArithmeticException:
Occurs when a number is divided by zero
e.g.: int c=20/0; ---> ArithmeticException
NumberFormatException:
Occurs an attempt is made to convert String with invalid digits
to number
e.g.: String s="123abc";
int x= Integer.parseInt(s); -- > NumberFormatException
NumberFormatException:
Occurs when a reference is used with out instance
E.g.: Rectangle r1; //Object is not initialized.
r1.display();
SSBN Degree College, ATP M Vishnuvardhan
Runtime Exceptions
ArrayIndexOutOfBoundsException:
Occurs when an array is referred beyond the boundaries
e.g: int a[]={10,20,30,40,50};
System.out.println(a[20]); //referring the array beyond boundaries
ArrayStoreException:
Occurs when an attempt is made to store wrong type of object
is stored in array of objects.
Eg: String s[]=new String [5];
s[0]=new Integer(75); // storing integer object in String array
StringIndexOutOfBoundsException:
Occurs when a String is referred beyond the string boundaries.
e.g. String s=“Java Program”;
char ch =s.charAt(23); // referring String beyond the
boundaries
SSBN Degree College, ATP M Vishnuvardhan
Exception Handling in Java
Exception handling in java can be done with the help of keywords
like try, catch, finally, throw, throws
try
{
-------
-------
}
catch(ExceptionClass 1)
{ }
catch(ExceptionClass 2)
{ }
----
-----
-----
catch(ExceptionClass n)
{ }
finally
{ }
SSBN Degree College, ATP M Vishnuvardhan
Exception Class Methods
 String getMessage(): returns the message returns
associated with the Exception
 String toString(): prints the exception name as well as
the message associated with the exception
 void printStackTrace(): prints the exception name,
message associated with it, line number in which the
exception is present and the method name where the
statement is present and class where the method is
located i.e..., full details
SSBN Degree College, ATP M Vishnuvardhan
finally block
A try block may contain any number of catch blocks but it contains
at most one finally block. The finally block is executed at least
once whether the exception occurs or not in the try block.
In general the try hold the statements like closing of files, closing
of streams, closing of the database connection and network
connections.
SSBN Degree College, ATP M Vishnuvardhan
User defined Exceptions
Java deals the general type of the exceptions which commonly
occur in the program. For an application type or a specific type of
exception we need to create our own exceptions. These exception
are called as user defined exceptions
Steps to create User defined exception
Create a class by extending from Exception class
If needed override any one of the Exception class methods
Check the condition for occurring of the exception
Create and object for the Exception and manually throw it using
throw keyword
SSBN Degree College, ATP M Vishnuvardhan
throw keyword
throw:- It is used to throw and exception from the try block
towards the catch blocks manually. Most of the it is used with user
defined exceptions where exception must be manually throw from
try block.
Syntax : throw new ExceptionClassName(<<Params>>);
E.g.: throw new ArithmeticException();
SSBN Degree College, ATP M Vishnuvardhan
throws keyword
throws:- It is generally used to postpone the exception handling.
Generally used at the method declaration to list the exception
which occur in the method. Any one who uses the method must
and should handle the exception listed
E.g.: public void test () throws IOException
{
======
}
public static void main(String args[])
{
try
{
test()
----
}
catch(IOException e)
{
---
}
}
SSBN Degree College, ATP M Vishnuvardhan
Questions

More Related Content

PPSX
Exception Handling
PPTX
Exception Handling in Java
PDF
Java exception handling ppt
PPTX
Exceptionhandling
PPTX
Exception handling
PPTX
Exception handling in JAVA
PPTX
Exception handling in java
PPT
Types of exceptions
Exception Handling
Exception Handling in Java
Java exception handling ppt
Exceptionhandling
Exception handling
Exception handling in JAVA
Exception handling in java
Types of exceptions

What's hot (20)

PDF
Java - Exception Handling Concepts
PPTX
PPT
Abstract class in java
PPT
Exception Handling in JAVA
PPTX
Java abstract class & abstract methods
PPTX
Java exception handling
ODP
Multithreading In Java
PPTX
Java Lambda Expressions.pptx
PPTX
I/O Streams
PPTX
Exception handling in java
PPTX
Core java complete ppt(note)
PPTX
Exception handling in java
PPT
C# Exceptions Handling
PPTX
Java exception handling
PPTX
Abstraction in java.pptx
PDF
Java threads
ODP
Exception Handling In Java
PPTX
Presentation on-exception-handling
PPTX
Multi-threaded Programming in JAVA
Java - Exception Handling Concepts
Abstract class in java
Exception Handling in JAVA
Java abstract class & abstract methods
Java exception handling
Multithreading In Java
Java Lambda Expressions.pptx
I/O Streams
Exception handling in java
Core java complete ppt(note)
Exception handling in java
C# Exceptions Handling
Java exception handling
Abstraction in java.pptx
Java threads
Exception Handling In Java
Presentation on-exception-handling
Multi-threaded Programming in JAVA
Ad

Similar to Exception handling (20)

PPTX
Exception handling
PPT
Exception handling
PDF
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
PDF
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
PDF
Class notes(week 8) on exception handling
PPTX
java exception.pptx
PPT
Exception handling
PPT
Exception handling
PPTX
UNIT-3.pptx Exception Handling and Multithreading
PPT
Exception Handling Java
PPTX
Training material exceptions v1
PPTX
Module 4-packages and exceptions in java.pptx
PPTX
Java Exceptions and Exception Handling
PPTX
Exception Handling In Java Presentation. 2024
PPTX
Java-Exception Handling Presentation. 2024
PPTX
UNIT 2.pptx
PPT
Computer Object Oriented Programming - Chapter 4 - Excption Handling.ppt
PPTX
unit 4 msbte syallbus for sem 4 2024-2025
PPTX
Java -Exception handlingunit-iv
PPT
Md07 exceptions&assertion
Exception handling
Exception handling
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
Class notes(week 8) on exception handling
java exception.pptx
Exception handling
Exception handling
UNIT-3.pptx Exception Handling and Multithreading
Exception Handling Java
Training material exceptions v1
Module 4-packages and exceptions in java.pptx
Java Exceptions and Exception Handling
Exception Handling In Java Presentation. 2024
Java-Exception Handling Presentation. 2024
UNIT 2.pptx
Computer Object Oriented Programming - Chapter 4 - Excption Handling.ppt
unit 4 msbte syallbus for sem 4 2024-2025
Java -Exception handlingunit-iv
Md07 exceptions&assertion
Ad

More from M Vishnuvardhan Reddy (20)

PPTX
Python Sets_Dictionary.pptx
PPTX
Lists_tuples.pptx
PPTX
Python Control Structures.pptx
PPTX
Python Strings.pptx
PPTX
Python Basics.pptx
PPTX
Python Operators.pptx
PPTX
Python Datatypes.pptx
PPTX
DataScience.pptx
PPT
PPT
Cascading Style Sheets
PPT
Java Threads
PPT
Java Streams
PPT
Scanner class
PPT
Polymorphism
PPT
PPT
Java applets
PPT
Control structures
PPT
Constructors
PPT
Classes&amp;objects
PPS
Python Sets_Dictionary.pptx
Lists_tuples.pptx
Python Control Structures.pptx
Python Strings.pptx
Python Basics.pptx
Python Operators.pptx
Python Datatypes.pptx
DataScience.pptx
Cascading Style Sheets
Java Threads
Java Streams
Scanner class
Polymorphism
Java applets
Control structures
Constructors
Classes&amp;objects

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPT
Teaching material agriculture food technology
PDF
Encapsulation theory and applications.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Approach and Philosophy of On baking technology
Machine learning based COVID-19 study performance prediction
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Understanding_Digital_Forensics_Presentation.pptx
Teaching material agriculture food technology
Encapsulation theory and applications.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Reach Out and Touch Someone: Haptics and Empathic Computing
Unlocking AI with Model Context Protocol (MCP)
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Advanced methodologies resolving dimensionality complications for autism neur...
Network Security Unit 5.pdf for BCA BBA.
20250228 LYD VKU AI Blended-Learning.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Big Data Technologies - Introduction.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Approach and Philosophy of On baking technology

Exception handling

  • 1. Exception Handling In JAVA -M Vishnuvardhan, Dept. of Computer Science, SSBN Degree College, ATP
  • 2. SSBN Degree College, ATP M Vishnuvardhan Introduction In general a program contains the following types of errors a)Compile-Time Errors b)Run-Time Errors
  • 3. SSBN Degree College, ATP M Vishnuvardhan Compile-Time Errors Compile time errors are mainly due to the following reasons a)Misspelt of the keywords i.e., float x,y; b)Wrong Identifier names i.e., int avg?marks; c)Wrong Syntax i.e., missing of semicolon … d)Wrong semantics i.e., int x=3.14; These errors are identified by the compiler and can be rectified very easily
  • 4. SSBN Degree College, ATP M Vishnuvardhan Run-Time Errors In programs there are certain types of errors which are not found at compilation time. These are known at the time of running. The compiler cant find these category of errors. Run-time errors are mainly due to the wrong input and wrong logic in the program. Run-time errors are called as Exceptions. E.g., int a,b,c; a= 20; b=0; c= a/b;
  • 5. SSBN Degree College, ATP M Vishnuvardhan Exception - Defined An desirable/ abnormal situation which prevail inside the program is called as an Exception. In general when ever exception occurs the program suddenly stops. This leads to a major problem when working with files, databases, network connections…. Exception handling is a technique or mechanism of handling seriousness of the exception. Traditional programming languages lack of exception handling mechanism. But all modern programming languages are capable of handling the exception Robust programming language
  • 6. SSBN Degree College, ATP M Vishnuvardhan Exceptions in java The undesirable condition in java are categorized in to two types 1.Error – Hardware related can’t be handled from the program 2.Exception – Software related can be handled from the program Object Throwable Error Exception StackOver FlowError Virtual MachineError Checked Exception Unchecked Exception UserDefined Exception
  • 7. SSBN Degree College, ATP M Vishnuvardhan Categories of Exceptions Exception is the base class of all types of the Exceptions Checked Exception:- Must be handled by the programmer other wise compile time error is generated Unchecked Exception:- It may or may not be handled by the programmer no compile time error is generated. RunTimeException is the base class of all unchecked Exceptions User Defined Exceptions:- In general java deals with common types of exception for a specific type of exception. The programmer has to define the exceptions of the specific type those are called as User-defined Exceptions
  • 8. SSBN Degree College, ATP M Vishnuvardhan Checked Exceptions Exception FileNotFoundException IllegalThreadStateException InterruptedException IOException SQLException
  • 9. SSBN Degree College, ATP M Vishnuvardhan Checked Exceptions FileNotFoundException: Occurs when an attempt is made to open a file which is not actually present. IllegalThreadStateException: Occurs when a thread is moved to an illegal state InterruptedException: Occurs when a Thread is interrupted. IOException: Occurs due to the failure of the h/w or s/w associated with the I/O devices etc., SQLException: Occurs when a database connection is not available or when it returns null.
  • 10. SSBN Degree College, ATP M Vishnuvardhan RunTimeException RunTimeException ArithmeticException NumberFormatException NullPointerException IndexOutOfBoundsException ArrayIndexOutOfBoundsException StringIndexOutOfBoundsException
  • 11. SSBN Degree College, ATP M Vishnuvardhan Runtime Exceptions ArithmeticException: Occurs when a number is divided by zero e.g.: int c=20/0; ---> ArithmeticException NumberFormatException: Occurs an attempt is made to convert String with invalid digits to number e.g.: String s="123abc"; int x= Integer.parseInt(s); -- > NumberFormatException NumberFormatException: Occurs when a reference is used with out instance E.g.: Rectangle r1; //Object is not initialized. r1.display();
  • 12. SSBN Degree College, ATP M Vishnuvardhan Runtime Exceptions ArrayIndexOutOfBoundsException: Occurs when an array is referred beyond the boundaries e.g: int a[]={10,20,30,40,50}; System.out.println(a[20]); //referring the array beyond boundaries ArrayStoreException: Occurs when an attempt is made to store wrong type of object is stored in array of objects. Eg: String s[]=new String [5]; s[0]=new Integer(75); // storing integer object in String array StringIndexOutOfBoundsException: Occurs when a String is referred beyond the string boundaries. e.g. String s=“Java Program”; char ch =s.charAt(23); // referring String beyond the boundaries
  • 13. SSBN Degree College, ATP M Vishnuvardhan Exception Handling in Java Exception handling in java can be done with the help of keywords like try, catch, finally, throw, throws try { ------- ------- } catch(ExceptionClass 1) { } catch(ExceptionClass 2) { } ---- ----- ----- catch(ExceptionClass n) { } finally { }
  • 14. SSBN Degree College, ATP M Vishnuvardhan Exception Class Methods  String getMessage(): returns the message returns associated with the Exception  String toString(): prints the exception name as well as the message associated with the exception  void printStackTrace(): prints the exception name, message associated with it, line number in which the exception is present and the method name where the statement is present and class where the method is located i.e..., full details
  • 15. SSBN Degree College, ATP M Vishnuvardhan finally block A try block may contain any number of catch blocks but it contains at most one finally block. The finally block is executed at least once whether the exception occurs or not in the try block. In general the try hold the statements like closing of files, closing of streams, closing of the database connection and network connections.
  • 16. SSBN Degree College, ATP M Vishnuvardhan User defined Exceptions Java deals the general type of the exceptions which commonly occur in the program. For an application type or a specific type of exception we need to create our own exceptions. These exception are called as user defined exceptions Steps to create User defined exception Create a class by extending from Exception class If needed override any one of the Exception class methods Check the condition for occurring of the exception Create and object for the Exception and manually throw it using throw keyword
  • 17. SSBN Degree College, ATP M Vishnuvardhan throw keyword throw:- It is used to throw and exception from the try block towards the catch blocks manually. Most of the it is used with user defined exceptions where exception must be manually throw from try block. Syntax : throw new ExceptionClassName(<<Params>>); E.g.: throw new ArithmeticException();
  • 18. SSBN Degree College, ATP M Vishnuvardhan throws keyword throws:- It is generally used to postpone the exception handling. Generally used at the method declaration to list the exception which occur in the method. Any one who uses the method must and should handle the exception listed E.g.: public void test () throws IOException { ====== } public static void main(String args[]) { try { test() ---- } catch(IOException e) { --- } }
  • 19. SSBN Degree College, ATP M Vishnuvardhan Questions