The Artima Developer Community
Sponsored Link

Java Buzz Forum
Multiple catch blocks in java example

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Multiple catch blocks in java example Posted: Apr 25, 2016 1:49 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Multiple catch blocks in java example
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
Is there any chance of getting multiple exception?

  • Lets see a java example programs which can raise multiple exceptions.


  1. package exceptions;
  2. public class MultipleCatchBlocks {
  3.  
  4.     /**
  5.      * @www.instanceofjava.com
  6.      */
  7.  
  8. public static void main(String[] args) {
  9.         
  10.         
  11.  int x=10;
  12.  int y=0;
  13.  
  14. try{
  15.             
  16.    int i=x/y;
  17.    int a[]=new int[2];
  18.    a[3]=12;
  19.             
  20. } catch(ArithmeticException e){
  21.             e.printStackTrace();
  22. }
  23.  
  24. }
  25.  
  26. }

Output:


  1. java.lang.ArithmeticException: / by zero
        at exceptions.MultipleCatchBlocks.main(MultipleCatchBlocks.java:15)

  •  Lets see second case:


multiple catch blocks in java



  • In the 1st program there is a chance of getting two types of exceptions and we kept only one catch block with  ArithmeticException e . 
  • But there will be chance of getting ArrayIndexOutOfBoundsException too. 
  • So we need to keep multiple catch blocks in order to handle all those exceptions
  • Or else we can keep single catch block with super class Exception class object.

 




Multiple catch blocks for single try block:

 

  • One try can have multiple catch blocks 
  • Every try should and must be associated with at least one catch block.( try without catch)
  • Whenever an exception object is identified in try block and if there are multiple catch blocks then the priority for the catch block would be given based on order in which catch blocks are have been defined.
  • Highest priority would be always given to first catch block . If the first catch block can not handle the identified exception object then it considers the immediate next catch block.

Disadvantages of multiple catch blocks:
 
  •  We have to know the names of every corresponding exception class names.
  • We have to understand which statement is generating which exception class object.

    Solution: 

    •  Define a single catch block so that it should be able to handle any kind of exceptions identified in the try block.
    • Exception class is the super class for all the exception classes.
    • To the reference of super class object we can assign any sub class object.

    Read: Multiple catch blocks in java example

    Topic: Performance vs Reliability: Why Java Apps are Like F1 Cars Previous Topic   Next Topic Topic: Code Review and Single Responsibility Principle

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use