Exception handling in java-
Mrs.S.S.Patil Assistant Professor
SITCOE,Yadrav
• Exception handling is the abnormal condition that occur during execution of program to stop the entire flow of application
called as “Exception handling.”
• Why?
• package com.test;
•
• public class Test {
•
• public static void main(String[] args) {
•
• System.out.println("First line");
• System.out.println("Second line");
• System.out.println("Third line");
•
• }
•
• }
•
• Output-
• First line
• Second line
• Third line
• Now we add one exception line code as below
• package com.test;
• public class Test {
• public static void main(String[] args) {
•
• System.out.println("First line");
• System.out.println("Second line");
• System.out.println("Third line");
• int a = 10 / 0;
• System.out.println("Fourth line");
• System.out.println("Fifth line");
•
• }
• }
• Output-
• First lineException in thread "main"
• Second line
• Third line
• java.lang.ArithmeticException: / by zero
• at com.test.Test.main(Test.java:10)
•
• In this example, two statements are not executed, if you want to execute that two statements then how to do it in java? Then you should go
for exception handling.
• package com.test;
• public class Test {
• public static void main(String[] args) {
• System.out.println("First line");
• System.out.println("Second line");
• System.out.println("Third line");
• try
• {
• int a = 10 / 0;
• } catch (Exception e)
• {
• System.out.println(e);
• }
•
• System.out.println("Fourth line");
• System.out.println("Fifth line");
•
• }
• }
• Output-
• First line
• Second line
• Third line
• java.lang.ArithmeticException: / by zero
• Fourth line
• Fifth line
• Exception hierarchy-
• Throwable-
• In the above given Hierarchy Throwable is a class which is at the top of the exception hierarchy, from which all exception classes are derived.
• It is the super class of all Exceptions in Java.
• Both Exception and Errors are java classes which are derived from the Throwable class.
• Error-
• Error is subclass of throwable class.
• Errors are mostly the abnormal conditions.
• Error does not occur because of the programmer’s mistakes, but when system is not working properly or a resource is not allocated properly.
• Memory out of bound exception, stack overflow etc., are examples of Error.
• Difference between Checked and Unchecked Exceptions
• 1) Checked Exception
• The classes which directly inherit Throwable class except RuntimeException and Error are known as checked exceptions.
• The exceptions which are checked by the compiler whether programmer handling or not, for smooth execution of the program at runtime, are called
checked exceptions.
• Checked exceptions are checked at compile-time.
• Example-
• IOException
• SQLException
• 2) Unchecked Exception
• The classes which inherit RuntimeException are known as unchecked exceptions.
• Unchecked exceptions are not checked by the compiler whether programmer handing or not, but they are checked at runtime.
• Example-
• ArithmeticException-
• 3) Error
• Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
•
• StackOverFlowError:
• It is the child class of Error and hence it is unchecked. Whenever we are trying to invoke recursive method call JVM will
raise StackOverFlowError automatically. Example:
• class Test {
• public static void methodOne() {
• methodTwo();
• }
•
• public static void methodTwo() {
• methodOne();
• }
•
• public static void main(String[] args) {
• methodOne();
• }
• }
•
• Output: Run time error: StackOverFlowError
• Possible way to write try catch block
• 1.
• try {
• //not allowed
• }
• 2.
• try {
• //allowed
• }
• catch(Exception e){
• }
• 3.
• try {
• //allowed
• }
• finally{
• }
• 4.
• try{
• //allowed
• }
• catch (Exception e){
• }
• finally{
• 5.
• try {
• //allowed
• }
• Catch(ArithmaticException e1){
• }
• Catch(Exception e){
• }
•
• 6.
• try {
• //not allowed
• }
• Catch(Exception e){
• }
• Catch(ArithmaticException e1){
• }
• Not allowed – Reason: The bigger exception cannot be in the first catch because it will accommodate all exceptions and there will
be no chance to reach the second catch of NullpointerException
Sr. No. Catch Finally
1 Catch block handles the error when it occurs in try
block
There is no need of exception thrown by try block
2 Catch block is executed only when the if exception is
thrown by try block, otherwise it is not executed
Finally block is always executed whether exception
occurs or not
3 We can use multiple catch block for only one try block Only one finally block is used for one try block
4 We can handle multiple exceptions by using catch
blocks
It is not for exception handling
finally-
The finally block is used when an important part of the code needs to be executed. It is always executed whether or not the exceptions are handled.
•Finally block will always get executed until we shut down JVM. To shut down JVM in java we call System.exit ().
•If you write this in try block in that case finally block will not be executed.
•Normally, finally block contains the code to release resources like DB connections, IO streams etc
Question. What is the difference Between Catch and finally in java?
• How to create the custom exception in java:-
• We can create our own Exception that is known as custom exception or user-defined
exception.
• By the help of custom exception, you can have your own exception and message.
• Example-1- Scenario
• Sometimes it is required to develop meaningful exceptions based on application
requirements. For example suppose you have one savings account in SBI Bank and you
have 50000 in your account. Suppose you attempted to withdraw 60000 from your
account. In java you can handle. You need to display some error message related to
insufficient fund.
• Steps to create the user defined exception
• Create the new class.
• The user defined exception class must extend
from java.lang.Exception or java.lang.RunTimeException class.
• While creating custom exception, prefer to create an unchecked, Runtime exception than
a checked exception.
• Every user defined exception class in which parametrized Constructor must called
parametrized Constructor of
either java.lang.Exception or java.lang.RunTimeException class by using super(string
parameter always).
• package com.Test;
•
• public class InsufficientFundException extends RuntimeException {
•
• private String message;
•
• public InsufficientFundException(String message) {
• //this.message = message;
• super(message);
• }
• }
•
• package com.Test;
•
• public class Account {
•
• private int balance = 3000;
•
• public int balance() {
• return balance;
• }
•
• public void withdraw(int amount) {
• if (amount > balance) {
• throw new InsufficientFundException("Insufficient balance in your account..");
• }
• balance = balance - amount;
• }
• }
• package com.Test;
•
• public class MainTest {
•
• public static void main(String[] args) {
•
• Account account = new Account();
• System.out.println("Current balance : " + account.balance());
• account.withdraw(3500);
• System.out.println("Current balance : " + account.balance());
• }
• }
•

More Related Content

PPT
Exceptions
PPTX
java exception.pptx
PPTX
Exception handling in JAVA
PPTX
Exception‐Handling in object oriented programming
PDF
Ch-1_5.pdf this is java tutorials for all
PPTX
Exception handling and throw and throws keyword in java.pptx
PPT
Java Exception Handling & IO-Unit-3 (1).ppt
PPTX
Java exception handling
Exceptions
java exception.pptx
Exception handling in JAVA
Exception‐Handling in object oriented programming
Ch-1_5.pdf this is java tutorials for all
Exception handling and throw and throws keyword in java.pptx
Java Exception Handling & IO-Unit-3 (1).ppt
Java exception handling

Similar to Exception handling in java-PPT.pptx (20)

PPTX
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
DOCX
Java Exception handling
PPT
Exception handling
PPT
8.Exception handling latest(MB).ppt .
PDF
Class notes(week 8) on exception handling
DOCX
Exception handling in java
PPTX
OBJECT ORIENTED PROGRAMMING_Unit3_NOTES first half.pptx
PPTX
using Java Exception Handling in Java.pptx
PPT
Exception handling
PPTX
Unit 4 exceptions and threads
PPTX
Java-Exception Handling Presentation. 2024
PPTX
Exception Handling In Java Presentation. 2024
PPT
06 exceptions
PPTX
L14 exception handling
PDF
Java exceptions
PPT
Exception Handling in JAVA
PPT
A36519192_21789_4_2018_Exception Handling.ppt
PPTX
Exception handling in java
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
Java Exception handling
Exception handling
8.Exception handling latest(MB).ppt .
Class notes(week 8) on exception handling
Exception handling in java
OBJECT ORIENTED PROGRAMMING_Unit3_NOTES first half.pptx
using Java Exception Handling in Java.pptx
Exception handling
Unit 4 exceptions and threads
Java-Exception Handling Presentation. 2024
Exception Handling In Java Presentation. 2024
06 exceptions
L14 exception handling
Java exceptions
Exception Handling in JAVA
A36519192_21789_4_2018_Exception Handling.ppt
Exception handling in java
Ad

More from sonalipatil225940 (6)

DOCX
Its Concept of Collection _JAVA_Collection2.docx
DOCX
JAVA Programming-Lectures-1_javaFeatures (2).docx
PPTX
Swing component point are mentioned in PPT which helpgul for creating Java GU...
PPTX
Java Programming Environment,JDK,JRE,JVM.pptx
PPTX
Introduction To Java history, application, features.pptx
PPT
Core Java Concept Exception Handling Part-II.ppt
Its Concept of Collection _JAVA_Collection2.docx
JAVA Programming-Lectures-1_javaFeatures (2).docx
Swing component point are mentioned in PPT which helpgul for creating Java GU...
Java Programming Environment,JDK,JRE,JVM.pptx
Introduction To Java history, application, features.pptx
Core Java Concept Exception Handling Part-II.ppt
Ad

Recently uploaded (20)

PPTX
Principal presentation for NAAC (1).pptx
PDF
Soil Improvement Techniques Note - Rabbi
PPTX
Feature types and data preprocessing steps
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PPTX
Petroleum Refining & Petrochemicals.pptx
PPTX
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
PDF
Unit1 - AIML Chapter 1 concept and ethics
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PDF
Introduction to Power System StabilityPS
PDF
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
PPTX
Measurement Uncertainty and Measurement System analysis
PDF
Design of Material Handling Equipment Lecture Note
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PPTX
Management Information system : MIS-e-Business Systems.pptx
PDF
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
PPTX
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PPTX
wireless networks, mobile computing.pptx
PDF
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
Principal presentation for NAAC (1).pptx
Soil Improvement Techniques Note - Rabbi
Feature types and data preprocessing steps
Exploratory_Data_Analysis_Fundamentals.pdf
Petroleum Refining & Petrochemicals.pptx
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
Unit1 - AIML Chapter 1 concept and ethics
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
Introduction to Power System StabilityPS
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
Measurement Uncertainty and Measurement System analysis
Design of Material Handling Equipment Lecture Note
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
Management Information system : MIS-e-Business Systems.pptx
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
distributed database system" (DDBS) is often used to refer to both the distri...
wireless networks, mobile computing.pptx
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
"Array and Linked List in Data Structures with Types, Operations, Implementat...

Exception handling in java-PPT.pptx

  • 1. Exception handling in java- Mrs.S.S.Patil Assistant Professor SITCOE,Yadrav
  • 2. • Exception handling is the abnormal condition that occur during execution of program to stop the entire flow of application called as “Exception handling.” • Why? • package com.test; • • public class Test { • • public static void main(String[] args) { • • System.out.println("First line"); • System.out.println("Second line"); • System.out.println("Third line"); • • } • • } • • Output- • First line • Second line • Third line
  • 3. • Now we add one exception line code as below • package com.test; • public class Test { • public static void main(String[] args) { • • System.out.println("First line"); • System.out.println("Second line"); • System.out.println("Third line"); • int a = 10 / 0; • System.out.println("Fourth line"); • System.out.println("Fifth line"); • • } • } • Output- • First lineException in thread "main" • Second line • Third line • java.lang.ArithmeticException: / by zero • at com.test.Test.main(Test.java:10) • • In this example, two statements are not executed, if you want to execute that two statements then how to do it in java? Then you should go for exception handling.
  • 4. • package com.test; • public class Test { • public static void main(String[] args) { • System.out.println("First line"); • System.out.println("Second line"); • System.out.println("Third line"); • try • { • int a = 10 / 0; • } catch (Exception e) • { • System.out.println(e); • } • • System.out.println("Fourth line"); • System.out.println("Fifth line"); • • } • } • Output- • First line • Second line • Third line • java.lang.ArithmeticException: / by zero • Fourth line • Fifth line
  • 6. • Throwable- • In the above given Hierarchy Throwable is a class which is at the top of the exception hierarchy, from which all exception classes are derived. • It is the super class of all Exceptions in Java. • Both Exception and Errors are java classes which are derived from the Throwable class. • Error- • Error is subclass of throwable class. • Errors are mostly the abnormal conditions. • Error does not occur because of the programmer’s mistakes, but when system is not working properly or a resource is not allocated properly. • Memory out of bound exception, stack overflow etc., are examples of Error. • Difference between Checked and Unchecked Exceptions • 1) Checked Exception • The classes which directly inherit Throwable class except RuntimeException and Error are known as checked exceptions. • The exceptions which are checked by the compiler whether programmer handling or not, for smooth execution of the program at runtime, are called checked exceptions. • Checked exceptions are checked at compile-time. • Example- • IOException • SQLException • 2) Unchecked Exception • The classes which inherit RuntimeException are known as unchecked exceptions. • Unchecked exceptions are not checked by the compiler whether programmer handing or not, but they are checked at runtime. • Example- • ArithmeticException-
  • 7. • 3) Error • Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc. • • StackOverFlowError: • It is the child class of Error and hence it is unchecked. Whenever we are trying to invoke recursive method call JVM will raise StackOverFlowError automatically. Example: • class Test { • public static void methodOne() { • methodTwo(); • } • • public static void methodTwo() { • methodOne(); • } • • public static void main(String[] args) { • methodOne(); • } • } • • Output: Run time error: StackOverFlowError
  • 8. • Possible way to write try catch block • 1. • try { • //not allowed • } • 2. • try { • //allowed • } • catch(Exception e){ • } • 3. • try { • //allowed • } • finally{ • } • 4. • try{ • //allowed • } • catch (Exception e){ • } • finally{
  • 9. • 5. • try { • //allowed • } • Catch(ArithmaticException e1){ • } • Catch(Exception e){ • } • • 6. • try { • //not allowed • } • Catch(Exception e){ • } • Catch(ArithmaticException e1){ • } • Not allowed – Reason: The bigger exception cannot be in the first catch because it will accommodate all exceptions and there will be no chance to reach the second catch of NullpointerException
  • 10. Sr. No. Catch Finally 1 Catch block handles the error when it occurs in try block There is no need of exception thrown by try block 2 Catch block is executed only when the if exception is thrown by try block, otherwise it is not executed Finally block is always executed whether exception occurs or not 3 We can use multiple catch block for only one try block Only one finally block is used for one try block 4 We can handle multiple exceptions by using catch blocks It is not for exception handling finally- The finally block is used when an important part of the code needs to be executed. It is always executed whether or not the exceptions are handled. •Finally block will always get executed until we shut down JVM. To shut down JVM in java we call System.exit (). •If you write this in try block in that case finally block will not be executed. •Normally, finally block contains the code to release resources like DB connections, IO streams etc Question. What is the difference Between Catch and finally in java?
  • 11. • How to create the custom exception in java:- • We can create our own Exception that is known as custom exception or user-defined exception. • By the help of custom exception, you can have your own exception and message. • Example-1- Scenario • Sometimes it is required to develop meaningful exceptions based on application requirements. For example suppose you have one savings account in SBI Bank and you have 50000 in your account. Suppose you attempted to withdraw 60000 from your account. In java you can handle. You need to display some error message related to insufficient fund. • Steps to create the user defined exception • Create the new class. • The user defined exception class must extend from java.lang.Exception or java.lang.RunTimeException class. • While creating custom exception, prefer to create an unchecked, Runtime exception than a checked exception. • Every user defined exception class in which parametrized Constructor must called parametrized Constructor of either java.lang.Exception or java.lang.RunTimeException class by using super(string parameter always).
  • 12. • package com.Test; • • public class InsufficientFundException extends RuntimeException { • • private String message; • • public InsufficientFundException(String message) { • //this.message = message; • super(message); • } • } •
  • 13. • package com.Test; • • public class Account { • • private int balance = 3000; • • public int balance() { • return balance; • } • • public void withdraw(int amount) { • if (amount > balance) { • throw new InsufficientFundException("Insufficient balance in your account.."); • } • balance = balance - amount; • } • }
  • 14. • package com.Test; • • public class MainTest { • • public static void main(String[] args) { • • Account account = new Account(); • System.out.println("Current balance : " + account.balance()); • account.withdraw(3500); • System.out.println("Current balance : " + account.balance()); • } • } •