SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Andere mochten auch

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
 

Andere mochten auch (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
 

Ähnlich wie 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
 

Ähnlich wie 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 (1)
54240326 (1)54240326 (1)
54240326 (1)
 
54240326 copy
54240326   copy54240326   copy
54240326 copy
 
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
 

Mehr von Vinod Kumar V H

Mehr von 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
 

Kürzlich hochgeladen

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Kürzlich hochgeladen (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

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”); } }