SlideShare una empresa de Scribd logo
1 de 29
Presented By,
Vinod Kumar V H
Types of Exception

      Checked Exception
      Unchecked Exception
Common scenarios of Exception Handling
 ArithmeticException


       int a=50/0;

 NullPointerException


       String s=null;
       System.out.println(s.length());
Common scenarios of Exception Handling
 NumberFormatException


        String s=“abc”;
        int i=Integer.parseInt(s);

 ArrayIndexOutOfBoundsException


        int a[]= new int[5];
        a[10]=50;
Keywords used Exception Handling
            try
            catch
            finally
            throw
            throws
try
  class sample
  {
    public static void main(String args[])
    {
         int data=50/0;
         System.out.println(“I love india”);
    }
  }
try
  class sample{
     public static void main(String args[]){
          try{
                   int data=50/0;
           }
          catch(ArithmeticException e)
          {
                   System.out.println(e);
          }
          System.out.println(“I love india”);
     }
  }
Multiple catch
class sample{
  public static void main(String args[]){
       try{
              int a[]=new int[5];
              int a[5]=30/0; }
catch(ArithmeticException e){s.o.p(“task 1”);}
catch(ArrayIndexOutOfBoundsExceptin e){s.o.p(“task 2”);}
catch(Exception e){s.o.p(“task completed”);}
System.out.println(“I love india”);
  }
}
Multiple catch
class sample{
  public static void main(String args[]){
        try{
               int a[]=new int[5];
               int a[5]=30/0; }
catch(Exception e){s.o.p(“task completed”);}
catch(ArithmeticException e){s.o.p(“task 1”);}
catch(ArrayIndexOutOfBoundsExceptin e){s.o.p(“task 2”);}
System.out.println(“I love india”);
  }
}
Nested try
class sample{
   public static void main(String args[]){
        try{
            try{
                 s.o.p(“divide”);
                 int a=40/0;
        }catch(ArithmeticExceptin e){s.o.p(e);}
        try{
                 int a[]=new int[5];
                 int a[5]=4;
        }catch(ArrayIndexOutOfBoundsExceptin e){
                 s.o.p(e);}
        s.o.p(“other statements”);
   }catch(Exception e){s.o.p(“exception handled”);}
s.o.p(“normal flow”);
   }
}
finally
 Exception doesn’t occur
  class sample{
  public static void main(String args[]){
       try{
               int data=25/5;
               s.o.p(data);}
catch(NullPointerException e){s.o.p(e);}
finally{s.o.p(“finally block is always excuted”);}
s.o.p(“I love india”);
  }
}
finally
 Exception occurred but not handled
  class sample{
  public static void main(String args[]){
       try{
               int data=25/0;
               s.o.p(data);}
catch(NullPointerException e){s.o.p(e);}
finally{s.o.p(“finally block is always excuted”);}
s.o.p(“I love india”);
  }
}
finally
  Exception occurred but handled
 class sample{
   public static void main(String args[]){
        try{
                int data=25/0;
                s.o.p(data);}
 catch(ArithmeticException e){s.o.p(e);}
 finally{s.o.p(“finally block is always excuted”);}
 s.o.p(“I love india”);
   }
 }
throw
class sample{
  static void validate(int age){
        if(age<18)
               throw new ArithmeticException(“not valid”);
        else
               System.out.println(“welcome to vote”);
        }
  pulic static void main(String args[]){
        validate(13);
        System.out.println(“I love india”);
  }
}
Exception Propagation
class sample{
   void m(){
        int data=50/0; }
   void n(){
        m(); }
   void p(){
        try{
                n();
        }catch(Exception e){s.o.p(“Exception handled”);}
}
public static void main(String args[]){
   sample obj=new sample();
   obj.p();
   System.out.println(“I love india”);
   }
}
Exception Propagation
class sample{
   void m(){
        throw new java.io.IOException(“device error”); }
   void n(){
        m(); }
   void p(){
        try{
                n();
        }catch(Exception e){s.o.p(“Exception handled”);}
}
public static void main(String args[]){
   sample obj=new sample();
   obj.p();
   System.out.println(“I love india”);
   }
}
throws
   Checked exception can be propagated by throws keyword
  import java.io.IOException;
  class sample{
     void m() throws IOException {
          throw new IOException(“device error”); }
     void n() throws IOException{
          m(); }
     void p(){
          try{
                  n();
          }catch(Exception e){s.o.p(“Exception handled”);}
  }
  public static void main(String args[]){
     sample obj=new sample();
     obj.p();
     System.out.println(“I love india”);
     }
  }
Handling the exception if exception occurs in the
method which declares an exception
   import java.io.*;
   class M{
     void method() throws IOException {
          throw new IOException(“device error”);}
     }
     class Test{
          publi static void main(String args[]){
          try{
                 M m=new M();
                 m.method();
          }catch(Exceptionn e){s.o.p(“exception handled”);}
          System.out.println(“I love india”);}
     }
Declaring the exception if exception doesn’t
occur in the method which declares an exception
   import java.io.*;
   class M{
     void method() throws IOException {
          System.out.println(“device operation perform”);}
     }
     class Test{
          publi static void main(String args[]) throws
     IOException{
                 M m=new M();
                 m.method();
          System.out.println(“I love india”);
     }
   }
Declaring the exception if exception occurs in the
method which declares an exception
    import java.io.*;
    class M{
      void method() throws IOException {
           throw new IOException(“device error”);}
      }
      class Test{
           publi static void main(String args[]) throws
      IOException{
                  M m=new M();
                  m.method();
           System.out.println(“I love india”);}
      }
    }
Exception Handling with Method
           Overriding
 Super class method doesn’t declare an exception
 Subclass overridden method can’t declare the checked
    exception
import java.io.*;
class Parent{
    void msg(){s.o.p(“parent”);}
    }
class Child extends Parent{
    void msg() throws IOException{
       System.out.println(“child”);
       }
    pulic static void main(String args[]){
       Parent p=new Child();
       p.msg();
    }
}
 Super class method doesn’t declare an exception
 Subclass overridden method can declare the unchecked
  exception
import java.io.*;
class Parent{
    void msg(){s.o.p(“parent”);}
    }
class Child extends Parent{
    void msg() throws ArithmeticException{
       System.out.println(“child”);
       }
    pulic static void main(String args[]){
       Parent p=new Child();
       p.msg();
    }
}
Super class method declares an exception
   Subclass overridden method declares parent exception
  import java.io.*;
  class Parent{
      void msg()throws ArithmeticException{s.o.p(“parent”);}
      }
  class Child extends Parent{
      void msg() throws Exception{System.out.println(“child”); }
      pulic static void main(String args[]){
         Parent p=new Child();
         try{
         p.msg();
         }catch(Exception e){}
      }
  }
Super class method declares an exception
   Subclass overridden method declares same exception
  import java.io.*;
  class Parent{
      void msg()throws Exception{s.o.p(“parent”);}
      }
  class Child extends Parent{
      void msg() throws Exception{System.out.println(“child”); }
      pulic static void main(String args[]){
         Parent p=new Child();
         try{
         p.msg();
         }catch(Exception e){}
      }
  }
Super class method declares an exception
    Subclass overridden method declares subclass exception
   import java.io.*;
   class Parent{
       void msg()throws Exception{s.o.p(“parent”);}
       }
   class Child extends Parent{
       void msg() throws ArithmeticException{s.o.p(“child”); }
       pulic static void main(String args[]){
          Parent p=new Child();
          try{
          p.msg();
          }catch(Exception e){}
       }
   }
Super class method declares an exception
    Subclass overridden method declares no exception
   import java.io.*;
   class Parent{
       void msg()throws Exception{s.o.p(“parent”);}
       }
   class Child extends Parent{
       void msg() {s.o.p(“child”); }
       pulic static void main(String args[]){
          Parent p=new Child();
          try{
          p.msg();
          }catch(Exception e){}
       }
   }
Custom Exception
class sample{
  static void validate(int age) throws InvalidAgeException{
        if(age<18)
               throw new InvalidAgeException(“not valid”);
        else
               System.out.println(“welcome to vote”);
        }
  pulic static void main(String args[]){
        try{
        validate(13);
        }catch(Exception m){s.o.p(“Exception occured” +m);}
        System.out.println(“I love india”);
  }
}
Types of Exceptions and Exception Handling in Java

Más contenido relacionado

La actualidad más candente

Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)Alok Kumar
 
Exceptions and errors in Java
Exceptions and errors in JavaExceptions and errors in Java
Exceptions and errors in JavaManuela Grindei
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good TestsTomek Kaczanowski
 
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions Ganesh Samarthyam
 
Testing for Educational Gaming and Educational Gaming for Testing
Testing for Educational Gaming and Educational Gaming for TestingTesting for Educational Gaming and Educational Gaming for Testing
Testing for Educational Gaming and Educational Gaming for TestingTao Xie
 
Writing beautiful code with Java 8
Writing beautiful code with Java 8Writing beautiful code with Java 8
Writing beautiful code with Java 8Sergiu Mircea Indrie
 
Control statements
Control statementsControl statements
Control statementsraksharao
 
[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake
[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake
[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistakeFuture Processing
 
Simulado java se 7 programmer
Simulado java se 7 programmerSimulado java se 7 programmer
Simulado java se 7 programmerMiguel Vilaca
 

La actualidad más candente (13)

Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
 
Exceptions and errors in Java
Exceptions and errors in JavaExceptions and errors in Java
Exceptions and errors in Java
 
Java programs
Java programsJava programs
Java programs
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
 
Comp102 lec 5.1
Comp102   lec 5.1Comp102   lec 5.1
Comp102 lec 5.1
 
Testing for Educational Gaming and Educational Gaming for Testing
Testing for Educational Gaming and Educational Gaming for TestingTesting for Educational Gaming and Educational Gaming for Testing
Testing for Educational Gaming and Educational Gaming for Testing
 
Writing beautiful code with Java 8
Writing beautiful code with Java 8Writing beautiful code with Java 8
Writing beautiful code with Java 8
 
Control statements
Control statementsControl statements
Control statements
 
[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake
[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake
[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake
 
Simulado java se 7 programmer
Simulado java se 7 programmerSimulado java se 7 programmer
Simulado java se 7 programmer
 
Unit testing
Unit testingUnit testing
Unit testing
 
Exception
ExceptionException
Exception
 

Destacado

Sop - Packaging
Sop - PackagingSop - Packaging
Sop - PackagingTerry Koch
 
standard operating procedure pharmacy
standard operating procedure pharmacystandard operating procedure pharmacy
standard operating procedure pharmacysagar858
 
Standard Operating Procedure (SOP) for Information Technology (IT) Operations
Standard Operating Procedure (SOP) for Information Technology (IT) OperationsStandard Operating Procedure (SOP) for Information Technology (IT) Operations
Standard Operating Procedure (SOP) for Information Technology (IT) OperationsRonald Bartels
 
Standard operating procedure
Standard operating procedureStandard operating procedure
Standard operating procedureUMP
 

Destacado (6)

Sop - Packaging
Sop - PackagingSop - Packaging
Sop - Packaging
 
standard operating procedure pharmacy
standard operating procedure pharmacystandard operating procedure pharmacy
standard operating procedure pharmacy
 
Standard Operating Procedure (SOP) for Information Technology (IT) Operations
Standard Operating Procedure (SOP) for Information Technology (IT) OperationsStandard Operating Procedure (SOP) for Information Technology (IT) Operations
Standard Operating Procedure (SOP) for Information Technology (IT) Operations
 
SOP
SOPSOP
SOP
 
Standard operating procedure
Standard operating procedureStandard operating procedure
Standard operating procedure
 
Warehousing
WarehousingWarehousing
Warehousing
 

Similar a Types of Exceptions and Exception Handling in Java

Turbinando o compilador do Java 8
Turbinando o compilador do Java 8Turbinando o compilador do Java 8
Turbinando o compilador do Java 8Marcelo de Castro
 
Throwing.javaimport java.util.InputMismatchException; import jav.pdf
Throwing.javaimport java.util.InputMismatchException; import jav.pdfThrowing.javaimport java.util.InputMismatchException; import jav.pdf
Throwing.javaimport java.util.InputMismatchException; import jav.pdfaquazac
 
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Ayes Chinmay
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testingjeresig
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsTomek Kaczanowski
 
About java
About javaAbout java
About javaJay Xu
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfShashikantSathe3
 
Code javascript
Code javascriptCode javascript
Code javascriptRay Ray
 
Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4jeresig
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsTomek Kaczanowski
 

Similar a Types of Exceptions and Exception Handling in Java (20)

Turbinando o compilador do Java 8
Turbinando o compilador do Java 8Turbinando o compilador do Java 8
Turbinando o compilador do Java 8
 
Java.lang.object
Java.lang.objectJava.lang.object
Java.lang.object
 
Throwing.javaimport java.util.InputMismatchException; import jav.pdf
Throwing.javaimport java.util.InputMismatchException; import jav.pdfThrowing.javaimport java.util.InputMismatchException; import jav.pdf
Throwing.javaimport java.util.InputMismatchException; import jav.pdf
 
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
54240326 copy
54240326   copy54240326   copy
54240326 copy
 
54240326 (1)
54240326 (1)54240326 (1)
54240326 (1)
 
Migrating to JUnit 5
Migrating to JUnit 5Migrating to JUnit 5
Migrating to JUnit 5
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
 
About java
About javaAbout java
About java
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
 
Code javascript
Code javascriptCode javascript
Code javascript
 
Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 
Nalinee java
Nalinee javaNalinee java
Nalinee java
 
Java generics
Java genericsJava generics
Java generics
 
Lab4
Lab4Lab4
Lab4
 

Más de Vinod Kumar V H

Más de Vinod Kumar V H (6)

Apache Ant
Apache AntApache Ant
Apache Ant
 
Captcha
CaptchaCaptcha
Captcha
 
Team work & Interpersonal skills
Team work & Interpersonal skillsTeam work & Interpersonal skills
Team work & Interpersonal skills
 
Augmented Reality
Augmented RealityAugmented Reality
Augmented Reality
 
Thin Client
Thin ClientThin Client
Thin Client
 
Thin client
Thin clientThin client
Thin client
 

Último

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleCeline George
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 

Último (20)

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 

Types of Exceptions and Exception Handling in Java

  • 2. Types of Exception  Checked Exception  Unchecked Exception
  • 3. Common scenarios of Exception Handling  ArithmeticException int a=50/0;  NullPointerException String s=null; System.out.println(s.length());
  • 4. Common scenarios of Exception Handling  NumberFormatException String s=“abc”; int i=Integer.parseInt(s);  ArrayIndexOutOfBoundsException int a[]= new int[5]; a[10]=50;
  • 5. Keywords used Exception Handling  try  catch  finally  throw  throws
  • 6. try class sample { public static void main(String args[]) { int data=50/0; System.out.println(“I love india”); } }
  • 7. try class sample{ public static void main(String args[]){ try{ int data=50/0; } catch(ArithmeticException e) { System.out.println(e); } System.out.println(“I love india”); } }
  • 8. Multiple catch class sample{ public static void main(String args[]){ try{ int a[]=new int[5]; int a[5]=30/0; } catch(ArithmeticException e){s.o.p(“task 1”);} catch(ArrayIndexOutOfBoundsExceptin e){s.o.p(“task 2”);} catch(Exception e){s.o.p(“task completed”);} System.out.println(“I love india”); } }
  • 9. Multiple catch class sample{ public static void main(String args[]){ try{ int a[]=new int[5]; int a[5]=30/0; } catch(Exception e){s.o.p(“task completed”);} catch(ArithmeticException e){s.o.p(“task 1”);} catch(ArrayIndexOutOfBoundsExceptin e){s.o.p(“task 2”);} System.out.println(“I love india”); } }
  • 10. Nested try class sample{ public static void main(String args[]){ try{ try{ s.o.p(“divide”); int a=40/0; }catch(ArithmeticExceptin e){s.o.p(e);} try{ int a[]=new int[5]; int a[5]=4; }catch(ArrayIndexOutOfBoundsExceptin e){ s.o.p(e);} s.o.p(“other statements”); }catch(Exception e){s.o.p(“exception handled”);} s.o.p(“normal flow”); } }
  • 11. finally  Exception doesn’t occur class sample{ public static void main(String args[]){ try{ int data=25/5; s.o.p(data);} catch(NullPointerException e){s.o.p(e);} finally{s.o.p(“finally block is always excuted”);} s.o.p(“I love india”); } }
  • 12. finally  Exception occurred but not handled class sample{ public static void main(String args[]){ try{ int data=25/0; s.o.p(data);} catch(NullPointerException e){s.o.p(e);} finally{s.o.p(“finally block is always excuted”);} s.o.p(“I love india”); } }
  • 13. finally  Exception occurred but handled class sample{ public static void main(String args[]){ try{ int data=25/0; s.o.p(data);} catch(ArithmeticException e){s.o.p(e);} finally{s.o.p(“finally block is always excuted”);} s.o.p(“I love india”); } }
  • 14. throw class sample{ static void validate(int age){ if(age<18) throw new ArithmeticException(“not valid”); else System.out.println(“welcome to vote”); } pulic static void main(String args[]){ validate(13); System.out.println(“I love india”); } }
  • 15. Exception Propagation class sample{ void m(){ int data=50/0; } void n(){ m(); } void p(){ try{ n(); }catch(Exception e){s.o.p(“Exception handled”);} } public static void main(String args[]){ sample obj=new sample(); obj.p(); System.out.println(“I love india”); } }
  • 16. Exception Propagation class sample{ void m(){ throw new java.io.IOException(“device error”); } void n(){ m(); } void p(){ try{ n(); }catch(Exception e){s.o.p(“Exception handled”);} } public static void main(String args[]){ sample obj=new sample(); obj.p(); System.out.println(“I love india”); } }
  • 17. throws  Checked exception can be propagated by throws keyword import java.io.IOException; class sample{ void m() throws IOException { throw new IOException(“device error”); } void n() throws IOException{ m(); } void p(){ try{ n(); }catch(Exception e){s.o.p(“Exception handled”);} } public static void main(String args[]){ sample obj=new sample(); obj.p(); System.out.println(“I love india”); } }
  • 18. Handling the exception if exception occurs in the method which declares an exception import java.io.*; class M{ void method() throws IOException { throw new IOException(“device error”);} } class Test{ publi static void main(String args[]){ try{ M m=new M(); m.method(); }catch(Exceptionn e){s.o.p(“exception handled”);} System.out.println(“I love india”);} }
  • 19. Declaring the exception if exception doesn’t occur in the method which declares an exception import java.io.*; class M{ void method() throws IOException { System.out.println(“device operation perform”);} } class Test{ publi static void main(String args[]) throws IOException{ M m=new M(); m.method(); System.out.println(“I love india”); } }
  • 20. Declaring the exception if exception occurs in the method which declares an exception import java.io.*; class M{ void method() throws IOException { throw new IOException(“device error”);} } class Test{ publi static void main(String args[]) throws IOException{ M m=new M(); m.method(); System.out.println(“I love india”);} } }
  • 21. Exception Handling with Method Overriding
  • 22.  Super class method doesn’t declare an exception  Subclass overridden method can’t declare the checked exception import java.io.*; class Parent{ void msg(){s.o.p(“parent”);} } class Child extends Parent{ void msg() throws IOException{ System.out.println(“child”); } pulic static void main(String args[]){ Parent p=new Child(); p.msg(); } }
  • 23.  Super class method doesn’t declare an exception  Subclass overridden method can declare the unchecked exception import java.io.*; class Parent{ void msg(){s.o.p(“parent”);} } class Child extends Parent{ void msg() throws ArithmeticException{ System.out.println(“child”); } pulic static void main(String args[]){ Parent p=new Child(); p.msg(); } }
  • 24. Super class method declares an exception  Subclass overridden method declares parent exception import java.io.*; class Parent{ void msg()throws ArithmeticException{s.o.p(“parent”);} } class Child extends Parent{ void msg() throws Exception{System.out.println(“child”); } pulic static void main(String args[]){ Parent p=new Child(); try{ p.msg(); }catch(Exception e){} } }
  • 25. Super class method declares an exception  Subclass overridden method declares same exception import java.io.*; class Parent{ void msg()throws Exception{s.o.p(“parent”);} } class Child extends Parent{ void msg() throws Exception{System.out.println(“child”); } pulic static void main(String args[]){ Parent p=new Child(); try{ p.msg(); }catch(Exception e){} } }
  • 26. Super class method declares an exception  Subclass overridden method declares subclass exception import java.io.*; class Parent{ void msg()throws Exception{s.o.p(“parent”);} } class Child extends Parent{ void msg() throws ArithmeticException{s.o.p(“child”); } pulic static void main(String args[]){ Parent p=new Child(); try{ p.msg(); }catch(Exception e){} } }
  • 27. Super class method declares an exception  Subclass overridden method declares no exception import java.io.*; class Parent{ void msg()throws Exception{s.o.p(“parent”);} } class Child extends Parent{ void msg() {s.o.p(“child”); } pulic static void main(String args[]){ Parent p=new Child(); try{ p.msg(); }catch(Exception e){} } }
  • 28. Custom Exception class sample{ static void validate(int age) throws InvalidAgeException{ if(age<18) throw new InvalidAgeException(“not valid”); else System.out.println(“welcome to vote”); } pulic static void main(String args[]){ try{ validate(13); }catch(Exception m){s.o.p(“Exception occured” +m);} System.out.println(“I love india”); } }