SlideShare a Scribd company logo
Thanks for an Opportunity
NAME : GOVINDAN SAKKARABANI.,
.DEMO CLASS
Topic : Exception Handling in Java Program
Date : 21.11.2020
Time : 12.00 PM to 12.30 PM
Topics :
* Exception Handling Mechanism in Java
1. What is Exception.
2.Why Exception occurred in program.
3.How does Exception handling mechanism working in java.
4.Exception Types
5.Unchecked Exception
6.Checked Exception
7.Advantage of Exception
What is Exception
1. An exception in java is an abnormal condition or error that occurs at runtime execution and
interrupts (disrupts) the normal execution flow of the program.
2. An exception can be identified only at runtime, not at compile time.
3. If the exception object is not caught and handled properly, JVM will display an error message and will
terminate the rest of the program.
import java.util.*;
class Exception1{
public static void main (String[] args) {
int a = 10;
int b = 0;
int c = a/b; //number divisible by zero
System.out.println(c);
System.out.println("Welcome to Smart Training");
}
}
Example : Output :
Exception in thread "main"
java.lang.ArithmeticException: / by zero
at Exception1.main(Exception1.java:6):
Why Exception occurs in Program
1.Opening a non-existing file in your program.
2. Reading a file from a disk but the file does exist there.
3. Writing data to a disk but the disk is full or unformatted.
4. When the program asks for user input and the user enters invalid data.
5. When a user attempts to divide an integer value by zero, an exception occurs.
6. When a data stream is in an invalid format, etc.
Types of Exceptions
 Checked Exception
> Checked at Compile time.
> Must be either handled or specified using throws keyword.
 Unchecked Exception
> Not Checked at Compile time.
> Also called as a Runtime Exception.
 Errors
> Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
Exception Types :
How does Exception handling
mechanism work?
EXCEPTION HANDLING MECHANISM STEPS
1. Get the exception (Detects problem)
2. Throw exception (Inform that an error has occurred)
3. Catch the exception (Receive error information)
4. Handle exception (Take appropriate and corrective actions)
KEYWORDS
1. try
2. catch
3. finally
4. throw
5. throws
import java.util.*;
class Exception1{
public static void main (String[] args) {
try {
System.out.println("Before divide");
int a = 1/0;
System.out.println("After divide" +a);
}
catch(ArithmeticException e) {
System.out.println("A number cannot be divided by zero.");
}
System.out.println("Welcome to Smart Training");
}
}
Error handling code starts here
Exceptional case (Exception has occurred)
Exception handled, Here
User-friendly message
Runtime Exception Class (Unchecked Exception)
 ArithmeticException :
This exception is thrown when arithmetic problems, such as a number is divided by zero, is occurred. That is, it is caused by math's error.
 ClassCastException :
when we attempt to invalid typecasting in the program. That is, it is thrown when we cast an object to a subclass of which an object
is not an instance.
 NumberFormatException :
we try to convert a string into the numeric type and the process of illegal conversion fails. That is, It occurs due to the illegal
conversion of a string to a numeric format.
 ArrayIndexOutOfBoundsException :
ArrayIndexOutOfBoundsException exception is thrown when an array element is accessed out of the index.
 StringIndexOutOfBoundsException :
StringIndexOutOfBoundsException exception is thrown when a String or String Buffer element is accessed out of the index.
 NullPointerException :
when we attempt to use null instead of an object. That is, it is thrown when the reference is null.
Checked Exceptions in Java
 ClassNotFoundException :
We attempt to use a class that does not exist. Checked exceptions are those exceptions that are checked by the Java compiler itself.
 FileNotFoundException:
The FileNotFoundException is a checked exception that is thrown when we attempt to access a non-existing file..
 InterruptedException:
When a thread is in sleeping or waiting state and another thread attempt to interrupt it.
 IllegalAccessException:
When a method is called in another method or class but the calling method or class does not have permission to access
that method.
 NoSuchFieldException:
When an unknown variable is used in a program.
 NoSuchMethodException:
When the undefined method is used in a program.
Advantage of Exceptional Handling
1.The main advantage of exception handling technique is to
maintain the normal flow of the program.
2. It provides flexibility in handling situations of errors.
3. It allows us to define a user-friendly message to handle the
exception.
4. The exception handling technique helps to separate “Error-
Handling code” from “Regular code.”
Thanking You…
public class CheckedExceptionEx
{
public static void main(String[] args)
{
Thread.sleep(1000);
}
}
public class UncheckedException
{
public static void main(String[] args)
{
int x[ ] = {1, 2, 3, 4};
System.out.println(x[6]);
}
}
public class HelloWorld{
public static void main(String []args){
String s = null;
System.out.println(s.length());
}
}
public class HelloWorld{
public static void main(String []args){
int a = 10/0;
System.out.println(a);
}
}
public class HelloWorld{
public static void main(String []args){
int a[] = {10,20,30,40};
System.out.println(a[6]);
}
}
public class HelloWorld{
public static void main(String []args){
int a[] = {10,20,30,40};
System.out.println(a[6]);
}
}
public class HelloWorld{
public static void main(String []args){
int a[] = {10,20,30,40};
System.out.println(a[6]);
}
}
public class HelloWorld{
public static void main(String []args){
int a[] = {10,20,30,40};
System.out.println(a[6]);
}
}
Ad

More Related Content

What's hot (20)

Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
Adil Mehmoood
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
Elizabeth alexander
 
Exception handling
Exception handlingException handling
Exception handling
Raja Sekhar
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
pooja kumari
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
Java2Blog
 
exception handling
exception handlingexception handling
exception handling
Manav Dharman
 
Exception handling basic
Exception handling basicException handling basic
Exception handling basic
TharuniDiddekunta
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
Md.Shohel Rana ( M.Sc in CSE Khulna University of Engineering & Technology (KUET))
 
Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptions
myrajendra
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
gopalrajput11
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
Vadym Lotar
 
Built in exceptions
Built in exceptions Built in exceptions
Built in exceptions
TharuniDiddekunta
 
Exception handling
Exception handlingException handling
Exception handling
Shashwat Shriparv
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
Nuha Noor
 
Multi catch statement
Multi catch statementMulti catch statement
Multi catch statement
myrajendra
 
Java exception
Java exception Java exception
Java exception
Arati Gadgil
 
Java exception-handling
Java exception-handlingJava exception-handling
Java exception-handling
Suresh Kumar Reddy V
 
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
Kuntal Bhowmick
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Abhishek Pachisia
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
Kavitha713564
 

Similar to Java Exception Handling (20)

Exception Handling Multithreading: Fundamental of Exception; Exception types;...
Exception Handling Multithreading: Fundamental of Exception; Exception types;...Exception Handling Multithreading: Fundamental of Exception; Exception types;...
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
poongothai11
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Ankit Rai
 
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
SakkaravarthiS1
 
unit 4 msbte syallbus for sem 4 2024-2025
unit 4 msbte syallbus for sem 4 2024-2025unit 4 msbte syallbus for sem 4 2024-2025
unit 4 msbte syallbus for sem 4 2024-2025
AKSHAYBHABAD5
 
Exception‐Handling in object oriented programming
Exception‐Handling in object oriented programmingException‐Handling in object oriented programming
Exception‐Handling in object oriented programming
Parameshwar Maddela
 
Exception Hnadling java programming language
Exception Hnadling  java programming languageException Hnadling  java programming language
Exception Hnadling java programming language
ushakiranv110
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-iv
RubaNagarajan
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
primevideos176
 
Exception handling
Exception handlingException handling
Exception handling
Garuda Trainings
 
Exception handling
Exception handlingException handling
Exception handling
Garuda Trainings
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
pooja kumari
 
Exception Handling s the process of responding to unwanted or unexpected even...
Exception Handling s the process of responding to unwanted or unexpected even...Exception Handling s the process of responding to unwanted or unexpected even...
Exception Handling s the process of responding to unwanted or unexpected even...
jhgasdjkfsadjkfh
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
EduclentMegasoftel
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
Garuda Trainings
 
VTU MCA 2022 JAVA Exceeption Handling.docx
VTU MCA  2022 JAVA Exceeption Handling.docxVTU MCA  2022 JAVA Exceeption Handling.docx
VTU MCA 2022 JAVA Exceeption Handling.docx
Poornima E.G.
 
EXCEPTION HANDLING in prograaming
EXCEPTION HANDLING in prograamingEXCEPTION HANDLING in prograaming
EXCEPTION HANDLING in prograaming
MuskanNazeer
 
Itp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & AssertionsItp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & Assertions
phanleson
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
chauhankapil
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
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
Kuntal Bhowmick
 
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
Exception Handling Multithreading: Fundamental of Exception; Exception types;...Exception Handling Multithreading: Fundamental of Exception; Exception types;...
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
poongothai11
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Ankit Rai
 
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
SakkaravarthiS1
 
unit 4 msbte syallbus for sem 4 2024-2025
unit 4 msbte syallbus for sem 4 2024-2025unit 4 msbte syallbus for sem 4 2024-2025
unit 4 msbte syallbus for sem 4 2024-2025
AKSHAYBHABAD5
 
Exception‐Handling in object oriented programming
Exception‐Handling in object oriented programmingException‐Handling in object oriented programming
Exception‐Handling in object oriented programming
Parameshwar Maddela
 
Exception Hnadling java programming language
Exception Hnadling  java programming languageException Hnadling  java programming language
Exception Hnadling java programming language
ushakiranv110
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-iv
RubaNagarajan
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
primevideos176
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
pooja kumari
 
Exception Handling s the process of responding to unwanted or unexpected even...
Exception Handling s the process of responding to unwanted or unexpected even...Exception Handling s the process of responding to unwanted or unexpected even...
Exception Handling s the process of responding to unwanted or unexpected even...
jhgasdjkfsadjkfh
 
VTU MCA 2022 JAVA Exceeption Handling.docx
VTU MCA  2022 JAVA Exceeption Handling.docxVTU MCA  2022 JAVA Exceeption Handling.docx
VTU MCA 2022 JAVA Exceeption Handling.docx
Poornima E.G.
 
EXCEPTION HANDLING in prograaming
EXCEPTION HANDLING in prograamingEXCEPTION HANDLING in prograaming
EXCEPTION HANDLING in prograaming
MuskanNazeer
 
Itp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & AssertionsItp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & Assertions
phanleson
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
chauhankapil
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
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
Kuntal Bhowmick
 
Ad

Recently uploaded (20)

Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
Ad

Java Exception Handling

  • 1. Thanks for an Opportunity NAME : GOVINDAN SAKKARABANI., .DEMO CLASS Topic : Exception Handling in Java Program Date : 21.11.2020 Time : 12.00 PM to 12.30 PM
  • 2. Topics : * Exception Handling Mechanism in Java 1. What is Exception. 2.Why Exception occurred in program. 3.How does Exception handling mechanism working in java. 4.Exception Types 5.Unchecked Exception 6.Checked Exception 7.Advantage of Exception
  • 3. What is Exception 1. An exception in java is an abnormal condition or error that occurs at runtime execution and interrupts (disrupts) the normal execution flow of the program. 2. An exception can be identified only at runtime, not at compile time. 3. If the exception object is not caught and handled properly, JVM will display an error message and will terminate the rest of the program. import java.util.*; class Exception1{ public static void main (String[] args) { int a = 10; int b = 0; int c = a/b; //number divisible by zero System.out.println(c); System.out.println("Welcome to Smart Training"); } } Example : Output : Exception in thread "main" java.lang.ArithmeticException: / by zero at Exception1.main(Exception1.java:6):
  • 4. Why Exception occurs in Program 1.Opening a non-existing file in your program. 2. Reading a file from a disk but the file does exist there. 3. Writing data to a disk but the disk is full or unformatted. 4. When the program asks for user input and the user enters invalid data. 5. When a user attempts to divide an integer value by zero, an exception occurs. 6. When a data stream is in an invalid format, etc. Types of Exceptions  Checked Exception > Checked at Compile time. > Must be either handled or specified using throws keyword.  Unchecked Exception > Not Checked at Compile time. > Also called as a Runtime Exception.  Errors > Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
  • 6. How does Exception handling mechanism work? EXCEPTION HANDLING MECHANISM STEPS 1. Get the exception (Detects problem) 2. Throw exception (Inform that an error has occurred) 3. Catch the exception (Receive error information) 4. Handle exception (Take appropriate and corrective actions) KEYWORDS 1. try 2. catch 3. finally 4. throw 5. throws import java.util.*; class Exception1{ public static void main (String[] args) { try { System.out.println("Before divide"); int a = 1/0; System.out.println("After divide" +a); } catch(ArithmeticException e) { System.out.println("A number cannot be divided by zero."); } System.out.println("Welcome to Smart Training"); } } Error handling code starts here Exceptional case (Exception has occurred) Exception handled, Here User-friendly message
  • 7. Runtime Exception Class (Unchecked Exception)  ArithmeticException : This exception is thrown when arithmetic problems, such as a number is divided by zero, is occurred. That is, it is caused by math's error.  ClassCastException : when we attempt to invalid typecasting in the program. That is, it is thrown when we cast an object to a subclass of which an object is not an instance.  NumberFormatException : we try to convert a string into the numeric type and the process of illegal conversion fails. That is, It occurs due to the illegal conversion of a string to a numeric format.  ArrayIndexOutOfBoundsException : ArrayIndexOutOfBoundsException exception is thrown when an array element is accessed out of the index.  StringIndexOutOfBoundsException : StringIndexOutOfBoundsException exception is thrown when a String or String Buffer element is accessed out of the index.  NullPointerException : when we attempt to use null instead of an object. That is, it is thrown when the reference is null.
  • 8. Checked Exceptions in Java  ClassNotFoundException : We attempt to use a class that does not exist. Checked exceptions are those exceptions that are checked by the Java compiler itself.  FileNotFoundException: The FileNotFoundException is a checked exception that is thrown when we attempt to access a non-existing file..  InterruptedException: When a thread is in sleeping or waiting state and another thread attempt to interrupt it.  IllegalAccessException: When a method is called in another method or class but the calling method or class does not have permission to access that method.  NoSuchFieldException: When an unknown variable is used in a program.  NoSuchMethodException: When the undefined method is used in a program.
  • 9. Advantage of Exceptional Handling 1.The main advantage of exception handling technique is to maintain the normal flow of the program. 2. It provides flexibility in handling situations of errors. 3. It allows us to define a user-friendly message to handle the exception. 4. The exception handling technique helps to separate “Error- Handling code” from “Regular code.”
  • 11. public class CheckedExceptionEx { public static void main(String[] args) { Thread.sleep(1000); } } public class UncheckedException { public static void main(String[] args) { int x[ ] = {1, 2, 3, 4}; System.out.println(x[6]); } } public class HelloWorld{ public static void main(String []args){ String s = null; System.out.println(s.length()); } } public class HelloWorld{ public static void main(String []args){ int a = 10/0; System.out.println(a); } } public class HelloWorld{ public static void main(String []args){ int a[] = {10,20,30,40}; System.out.println(a[6]); } } public class HelloWorld{ public static void main(String []args){ int a[] = {10,20,30,40}; System.out.println(a[6]); } } public class HelloWorld{ public static void main(String []args){ int a[] = {10,20,30,40}; System.out.println(a[6]); } } public class HelloWorld{ public static void main(String []args){ int a[] = {10,20,30,40}; System.out.println(a[6]); } }
  翻译: