SlideShare a Scribd company logo
1 of 19
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

What's hot

Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in JavaAbhishek Pachisia
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaARAFAT ISLAM
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handlingNahian Ahmed
 
Exceptionhandling
ExceptionhandlingExceptionhandling
ExceptionhandlingNuha Noor
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in JavaJava2Blog
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handlingteach4uin
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptionsmyrajendra
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVAKunal Singh
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in javaGoogle
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++imran khan
 
Introduction to Java Programming Language
Introduction to Java Programming LanguageIntroduction to Java Programming Language
Introduction to Java Programming Languagejaimefrozr
 

What's hot (20)

Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling
Exception handlingException handling
Exception handling
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Exceptions handling notes in JAVA
Exceptions handling notes in JAVAExceptions handling notes in JAVA
Exceptions handling notes in JAVA
 
Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptions
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVA
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
 
Introduction to Java Programming Language
Introduction to Java Programming LanguageIntroduction to Java Programming Language
Introduction to Java Programming Language
 

Similar to Java Exception Handling Techniques

Exception handling
Exception handlingException handling
Exception handlingArdhendu Nandi
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling ConceptsVicter Paul
 
Exception handling
Exception handlingException handling
Exception handlingRaja Sekhar
 
Class notes(week 8) on exception handling
Class notes(week 8) on exception handlingClass notes(week 8) on exception handling
Class notes(week 8) on exception handlingKuntal Bhowmick
 
Exception handling
Exception handlingException handling
Exception handlingKarthik Sekar
 
UNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingUNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingSakkaravarthiS1
 
Exception Handling Java
Exception Handling JavaException Handling Java
Exception Handling Javaankitgarg_er
 
Training material exceptions v1
Training material   exceptions v1Training material   exceptions v1
Training material exceptions v1Shinu Suresh
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception HandlingMaqdamYasir
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024nehakumari0xf
 
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024kashyapneha2809
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-ivRubaNagarajan
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertionRakesh Madugula
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptionssaman Iftikhar
 
Introduction of exception in vb.net
Introduction of exception in vb.netIntroduction of exception in vb.net
Introduction of exception in vb.netsuraj pandey
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptxprimevideos176
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overviewBharath K
 

Similar to Java Exception Handling Techniques (20)

Exception handling
Exception handlingException handling
Exception handling
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
 
Exception handling
Exception handlingException handling
Exception handling
 
Class notes(week 8) on exception handling
Class notes(week 8) on exception handlingClass notes(week 8) on exception handling
Class notes(week 8) on exception handling
 
java exception.pptx
java exception.pptxjava exception.pptx
java exception.pptx
 
Exception handling
Exception handlingException handling
Exception handling
 
UNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingUNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and Multithreading
 
Exception Handling Java
Exception Handling JavaException Handling Java
Exception Handling Java
 
Training material exceptions v1
Training material   exceptions v1Training material   exceptions v1
Training material exceptions v1
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024
 
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-iv
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
 
Introduction of exception in vb.net
Introduction of exception in vb.netIntroduction of exception in vb.net
Introduction of exception in vb.net
 
Excception handling
Excception handlingExcception handling
Excception handling
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 

More from M Vishnuvardhan Reddy (20)

Python Sets_Dictionary.pptx
Python Sets_Dictionary.pptxPython Sets_Dictionary.pptx
Python Sets_Dictionary.pptx
 
Lists_tuples.pptx
Lists_tuples.pptxLists_tuples.pptx
Lists_tuples.pptx
 
Python Control Structures.pptx
Python Control Structures.pptxPython Control Structures.pptx
Python Control Structures.pptx
 
Python Strings.pptx
Python Strings.pptxPython Strings.pptx
Python Strings.pptx
 
Python Basics.pptx
Python Basics.pptxPython Basics.pptx
Python Basics.pptx
 
Python Operators.pptx
Python Operators.pptxPython Operators.pptx
Python Operators.pptx
 
Python Datatypes.pptx
Python Datatypes.pptxPython Datatypes.pptx
Python Datatypes.pptx
 
DataScience.pptx
DataScience.pptxDataScience.pptx
DataScience.pptx
 
Html forms
Html formsHtml forms
Html forms
 
Cascading Style Sheets
Cascading Style SheetsCascading Style Sheets
Cascading Style Sheets
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Scanner class
Scanner classScanner class
Scanner class
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Java intro
Java introJava intro
Java intro
 
Java applets
Java appletsJava applets
Java applets
 
Control structures
Control structuresControl structures
Control structures
 
Constructors
ConstructorsConstructors
Constructors
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
 
Shell sort
Shell sortShell sort
Shell sort
 

Recently uploaded

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 

Recently uploaded (20)

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 

Java Exception Handling Techniques

  • 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