<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: 221910303059</title>
    <description>The latest articles on DEV Community by 221910303059 (@221910303059).</description>
    <link>https://dev.to/221910303059</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F540906%2Fb8d27b08-6a1f-41cb-9ee0-a71621246a55.png</url>
      <title>DEV Community: 221910303059</title>
      <link>https://dev.to/221910303059</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/221910303059"/>
    <language>en</language>
    <item>
      <title>Handling Exception in Java</title>
      <dc:creator>221910303059</dc:creator>
      <pubDate>Tue, 15 Dec 2020 15:49:52 +0000</pubDate>
      <link>https://dev.to/221910303059/handling-exception-in-java-3ged</link>
      <guid>https://dev.to/221910303059/handling-exception-in-java-3ged</guid>
      <description>&lt;h1&gt;
  
  
  What is exception handling?
&lt;/h1&gt;

&lt;p&gt;An exception is an abnormal condition that disturbs the normal flow of the program. Exception handling is used in handling the runtime errors and maintains the continuity of the program.&lt;/p&gt;

&lt;h1&gt;
  
  
  Java exceptions
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Checked &lt;/li&gt;
&lt;li&gt;Unchecked&lt;/li&gt;
&lt;li&gt;Errors&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Checked&lt;/th&gt;
&lt;th&gt;Unchecked&lt;/th&gt;
&lt;th&gt;Errors&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Checked at compile-time&lt;/td&gt;
&lt;td&gt;Checked at run time&lt;/td&gt;
&lt;td&gt;Error remains the same&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;These classes directly inherit throwable class&lt;/td&gt;
&lt;td&gt;These classes inherit runtime exception&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ex:IOException,SQLException&lt;/td&gt;
&lt;td&gt;Ex:ArthemeticException,ArrayIndexOutOfBoundsException&lt;/td&gt;
&lt;td&gt;Ex:OutOfMemoryError,VirtualMachineError&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h1&gt;
  
  
  Try Block
&lt;/h1&gt;

&lt;p&gt;The statements which throw an exception are to be placed under try block&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It is suggested not to write unnecessary statements in the try block because the program terminates at a particular statement where an exception occurs.&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Catch Block
&lt;/h1&gt;

&lt;p&gt;It is used to handle the exception by declaring the type of exception within the parameter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Single or multiple catch blocks can be used after each single try block.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax of try-catch :
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; try{    
   //code that may throw an exception    
    }catch(Exception_class_Name ref){}  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Program using Try-Catch :
&lt;/h1&gt;

&lt;p&gt;public class TryCatch {  &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void main(String[] args) {  
    try  
    {  
    int arr[]= {2,4,6,8};  
    System.out.println(arr[10]);    
    }  

    catch(ArrayIndexOutOfBoundsException e)  
    {  
        System.out.println(e);  
    }  
    System.out.println("Program continues"); 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;java.lang.ArrayIndexOutOfBoundsException: 10&lt;br&gt;
Program continues&lt;/p&gt;

&lt;h1&gt;
  
  
  Program Using multiple catch blocks :
&lt;/h1&gt;

&lt;p&gt;public class Multiple {  &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void main(String[] args) {  

       try{    
            int a[]=new int[5];    

            System.out.println(a[10]);  
           }    
           catch(ArithmeticException e)  
              {  
               System.out.println("Arithmetic Exception occurs");  
              }    
           catch(ArrayIndexOutOfBoundsException e)  
              {  
               System.out.println("ArrayIndexOutOfBounds Exception occurs");  
              }    
           catch(Exception e)  
              {  
               System.out.println("Parent Exception occurs");  
              }             
           System.out.println("Program Continues");    
}  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;ArrayIndexOutOfBounds Exception occurs&lt;br&gt;
Program Continues&lt;/p&gt;

&lt;h1&gt;
  
  
  Finally Block
&lt;/h1&gt;

&lt;p&gt;The block of statements that are used to execute the important code. It is always executed whether an exception is handled or not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Program using finally block :
&lt;/h2&gt;

&lt;p&gt;public class Finally{&lt;br&gt;&lt;br&gt;
  public static void main(String args[]){&lt;br&gt;&lt;br&gt;
  try{&lt;br&gt;&lt;br&gt;
   int data=25/0;&lt;br&gt;&lt;br&gt;
   System.out.println(data);&lt;br&gt;&lt;br&gt;
  }&lt;br&gt;&lt;br&gt;
  catch(ArithmeticException e){&lt;br&gt;
    System.out.println(e);&lt;br&gt;
    }&lt;br&gt;&lt;br&gt;
  finally{&lt;br&gt;
    System.out.println("finally block is always executed");&lt;br&gt;
    }&lt;br&gt;&lt;br&gt;
  System.out.println("Program continues");&lt;br&gt;&lt;br&gt;
  }&lt;br&gt;&lt;br&gt;
}  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;java.lang.ArithmeticException: / by zero&lt;br&gt;
finally block is always executed&lt;br&gt;
Program continues&lt;/p&gt;

</description>
      <category>exception</category>
      <category>throws</category>
      <category>catch</category>
    </item>
  </channel>
</rss>
