SlideShare a Scribd company logo
1 of 24
Exception Handling
By
Sana Mateen
Exception Handling
• The exception handling in java is one of the powerful mechanism to
handle the runtime errors so that normal flow of the application can be
maintained.
• What is exception?
• Meaning: Exception is an abnormal condition.
• In java, exception is an event that disrupts the normal flow of the
program. It is an object which is thrown at runtime.
• What is exception handling?
• Exception Handling is a mechanism to handle runtime errors such as
ClassNotFound, IO, SQL, Remote etc.
Types of Exception
• There are mainly two types of
exceptions: checked and
unchecked where error is
considered as unchecked
exception. The sun microsystem
says there are three types of
exceptions:
• Checked Exception
• Unchecked Exception
• Error
Java try-catch
• Java try block is used to enclose the code that might throw an exception. It
must be used within the method.
• Java try block must be followed by either catch or finally block.
• Java catch block
• Java catch block is used to handle the Exception. It must be used after the try
block only.
• You can use multiple catch block with a single try.
Difference between checked and unchecked
exceptions
• 1) Checked Exception
• The classes that extend Throwable class except RuntimeException and
Error are known as checked exceptions e.g.IOException, SQLException
etc. Checked exceptions are checked at compile-time.
• 2) Unchecked Exception
• The classes that extend RuntimeException are known as unchecked
exceptions e.g. ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not
checked at compile-time rather they are checked at runtime.
• 3) Error
• Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError,
AssertionError etc.
• Common scenarios where exceptions may occur
• 1) Scenario where ArithmeticException occurs
• If we divide any number by zero, there occurs an ArithmeticException.
• int a=50/0;//ArithmeticException
• 2) Scenario where NullPointerException occurs
• If we have null value in any variable, performing any operation by the variable occurs an
NullPointerException.
• String s=null;
• System.out.println(s.length());//NullPointerException
• 3) Scenario where NumberFormatException occurs
• The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string
variable that have characters, converting this variable into digit will occur NumberFormatException.
• String s="abc";
• int i=Integer.parseInt(s);//NumberFormatException
• 4) Scenario where ArrayIndexOutOfBoundsException occurs
• If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as
shown below:
• int a[]=new int[5];
• a[10]=50; //ArrayIndexOutOfBoundsException
Termination or Resumptive Models
• There are two basic models in exception-handling theory.
• In termination the error is so critical there’s no way to get back to where
the exception occurred.
• The alternative is called resumption.
• It means that the exception handler is expected to do something to
rectify the situation and then the faulty method is retried, presuming
success the second time .
• If you want resumption, it means you still hope to continue execution
after the exception is handled.
throw keyword
• The Java throw keyword is used to explicitly throw an
exception.
• We can throw either checked or uncheked exception in java
by throw keyword. The throw keyword is mainly used to
throw custom exception.
• The syntax of java throw keyword is given below.
• throw exception;
• Let's see the example of throw IOException.
• throw new IOException("sorry device error);
throws keyword
• “Throws keyword” is mainly used for handling checked
exception as using throws we can declare multiple
exceptions in one go.
• The throws keyword is used in method declaration, in order
to explicitly specify the exceptions that a particular method
might throw.
• When a method declaration has one or more exceptions
defined using throws clause then the method-call must
handle all the defined exceptions.
Creating your own exceptions
• If you are creating your own Exception that is known as custom
exception or user-defined exception. Java custom exceptions are
used to customize the exception according to user need.
• By the help of custom exception, you can have your own exception
and message.
Advantages of Exception Handling
• Provision to Complete Program Execution:
• One of the important purposes of exception handling in Java is to continue program execution after
an exception is caught and handled. The execution of a Java program does not terminate when an
exception occurs. Once the exception is resolved, program execution continues till completion.
• By using well-structured try, catch, and finally blocks, you can create programs that fix exceptions and
continue execution as if there were no errors. If there is a possibility of more than one exception, you
can use multiple catch blocks to handle the different exceptions.
• Easy Identification of Program Code and Error-Handling Code
• Propagation of Errors
• Meaningful Error Reporting

More Related Content

What's hot

Exception Handling
Exception HandlingException Handling
Exception HandlingReddhi Basu
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVASURIT DATTA
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handlingNahian Ahmed
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
Control structures in java
Control structures in javaControl structures in java
Control structures in javaVINOTH R
 
Exception Handling
Exception HandlingException Handling
Exception Handlingbackdoor
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaPratik Soares
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in JavaAbhishek Pachisia
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handlingteach4uin
 
Exceptional Handling in Java
Exceptional Handling in JavaExceptional Handling in Java
Exceptional Handling in JavaQaziUmarF786
 
Java interfaces
Java interfacesJava interfaces
Java interfacesRaja Sekhar
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVAVINOTH R
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in JavaAnkit Rai
 
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in JavaAbhilash Nair
 

What's hot (20)

Exception Handling
Exception HandlingException Handling
Exception Handling
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Exception handling
Exception handling Exception handling
Exception handling
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception handling
Exception handlingException handling
Exception handling
 
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
 
Exceptional Handling in Java
Exceptional Handling in JavaExceptional Handling in Java
Exceptional Handling in Java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Java threads
Java threadsJava threads
Java threads
 
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in Java
 
Java threading
Java threadingJava threading
Java threading
 

Similar to Exceptionhandling

Java chapter 6
Java chapter 6Java chapter 6
Java chapter 6Abdii Rashid
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handlingraksharao
 
Java-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingJava-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingraksharao
 
A36519192_21789_4_2018_Exception Handling.ppt
A36519192_21789_4_2018_Exception Handling.pptA36519192_21789_4_2018_Exception Handling.ppt
A36519192_21789_4_2018_Exception Handling.pptpromila09
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptxprimevideos176
 
Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allHayomeTakele
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception HandlingMaqdamYasir
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVAKunal Singh
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaAdil Mehmoood
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingAboMohammad10
 
16 exception handling - i
16 exception handling - i16 exception handling - i
16 exception handling - iRavindra Rathore
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptxRDeepa9
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptxRDeepa9
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptxARUNPRANESHS
 
Exception handling
Exception handlingException handling
Exception handlingMinal Maniar
 
EXCEPTION HANDLING in prograaming
EXCEPTION HANDLING in prograamingEXCEPTION HANDLING in prograaming
EXCEPTION HANDLING in prograamingMuskanNazeer
 
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
Exception handlingException handling
Exception handlingpooja kumari
 
Chapter 5 Exception Handling (1).pdf
Chapter 5 Exception Handling (1).pdfChapter 5 Exception Handling (1).pdf
Chapter 5 Exception Handling (1).pdfFacultyAnupamaAlagan
 
exception -ppt.pptx
exception -ppt.pptxexception -ppt.pptx
exception -ppt.pptxDivyaKS18
 

Similar to Exceptionhandling (20)

Java chapter 6
Java chapter 6Java chapter 6
Java chapter 6
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handling
 
Java-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingJava-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handling
 
A36519192_21789_4_2018_Exception Handling.ppt
A36519192_21789_4_2018_Exception Handling.pptA36519192_21789_4_2018_Exception Handling.ppt
A36519192_21789_4_2018_Exception Handling.ppt
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
 
Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for all
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
 
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 Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
 
16 exception handling - i
16 exception handling - i16 exception handling - i
16 exception handling - i
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
 
Exception handling
Exception handlingException handling
Exception handling
 
EXCEPTION HANDLING in prograaming
EXCEPTION HANDLING in prograamingEXCEPTION HANDLING in prograaming
EXCEPTION HANDLING in prograaming
 
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
Exception handlingException handling
Exception handling
 
Chapter 5 Exception Handling (1).pdf
Chapter 5 Exception Handling (1).pdfChapter 5 Exception Handling (1).pdf
Chapter 5 Exception Handling (1).pdf
 
exception -ppt.pptx
exception -ppt.pptxexception -ppt.pptx
exception -ppt.pptx
 

More from Nuha Noor

Applets
AppletsApplets
AppletsNuha Noor
 
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...Nuha Noor
 
Understanding layout managers
Understanding layout managersUnderstanding layout managers
Understanding layout managersNuha Noor
 
Events1
Events1Events1
Events1Nuha Noor
 
Polymorphism
PolymorphismPolymorphism
PolymorphismNuha Noor
 
Packages
PackagesPackages
PackagesNuha Noor
 
Http request and http response
Http request and http responseHttp request and http response
Http request and http responseNuha Noor
 
Jsp elements
Jsp elementsJsp elements
Jsp elementsNuha Noor
 
Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessionsNuha Noor
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servletsNuha Noor
 
Reading init param
Reading init paramReading init param
Reading init paramNuha Noor
 

More from Nuha Noor (11)

Applets
AppletsApplets
Applets
 
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
 
Understanding layout managers
Understanding layout managersUnderstanding layout managers
Understanding layout managers
 
Events1
Events1Events1
Events1
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Packages
PackagesPackages
Packages
 
Http request and http response
Http request and http responseHttp request and http response
Http request and http response
 
Jsp elements
Jsp elementsJsp elements
Jsp elements
 
Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessions
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
 
Reading init param
Reading init paramReading init param
Reading init param
 

Recently uploaded

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 

Recently uploaded (20)

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 

Exceptionhandling

  • 2. Exception Handling • The exception handling in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained. • What is exception? • Meaning: Exception is an abnormal condition. • In java, exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime. • What is exception handling? • Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc.
  • 3.
  • 4. Types of Exception • There are mainly two types of exceptions: checked and unchecked where error is considered as unchecked exception. The sun microsystem says there are three types of exceptions: • Checked Exception • Unchecked Exception • Error
  • 5. Java try-catch • Java try block is used to enclose the code that might throw an exception. It must be used within the method. • Java try block must be followed by either catch or finally block. • Java catch block • Java catch block is used to handle the Exception. It must be used after the try block only. • You can use multiple catch block with a single try.
  • 6.
  • 7. Difference between checked and unchecked exceptions • 1) Checked Exception • The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time. • 2) Unchecked Exception • The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime. • 3) Error • Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
  • 8. • Common scenarios where exceptions may occur • 1) Scenario where ArithmeticException occurs • If we divide any number by zero, there occurs an ArithmeticException. • int a=50/0;//ArithmeticException • 2) Scenario where NullPointerException occurs • If we have null value in any variable, performing any operation by the variable occurs an NullPointerException. • String s=null; • System.out.println(s.length());//NullPointerException • 3) Scenario where NumberFormatException occurs • The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException. • String s="abc"; • int i=Integer.parseInt(s);//NumberFormatException • 4) Scenario where ArrayIndexOutOfBoundsException occurs • If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below: • int a[]=new int[5]; • a[10]=50; //ArrayIndexOutOfBoundsException
  • 9. Termination or Resumptive Models • There are two basic models in exception-handling theory. • In termination the error is so critical there’s no way to get back to where the exception occurred. • The alternative is called resumption. • It means that the exception handler is expected to do something to rectify the situation and then the faulty method is retried, presuming success the second time . • If you want resumption, it means you still hope to continue execution after the exception is handled.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14. throw keyword • The Java throw keyword is used to explicitly throw an exception. • We can throw either checked or uncheked exception in java by throw keyword. The throw keyword is mainly used to throw custom exception. • The syntax of java throw keyword is given below. • throw exception; • Let's see the example of throw IOException. • throw new IOException("sorry device error);
  • 15.
  • 16. throws keyword • “Throws keyword” is mainly used for handling checked exception as using throws we can declare multiple exceptions in one go. • The throws keyword is used in method declaration, in order to explicitly specify the exceptions that a particular method might throw. • When a method declaration has one or more exceptions defined using throws clause then the method-call must handle all the defined exceptions.
  • 17.
  • 18.
  • 19.
  • 20. Creating your own exceptions • If you are creating your own Exception that is known as custom exception or user-defined exception. Java custom exceptions are used to customize the exception according to user need. • By the help of custom exception, you can have your own exception and message.
  • 21.
  • 22.
  • 23. Advantages of Exception Handling • Provision to Complete Program Execution: • One of the important purposes of exception handling in Java is to continue program execution after an exception is caught and handled. The execution of a Java program does not terminate when an exception occurs. Once the exception is resolved, program execution continues till completion. • By using well-structured try, catch, and finally blocks, you can create programs that fix exceptions and continue execution as if there were no errors. If there is a possibility of more than one exception, you can use multiple catch blocks to handle the different exceptions.
  • 24. • Easy Identification of Program Code and Error-Handling Code • Propagation of Errors • Meaningful Error Reporting