<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rocking Team &#187; Exception Handling</title>
	<atom:link href="http://www.rockingteam.com/tag/exception-handling/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rockingteam.com</link>
	<description>Technology @ Your Finger Tips... Computer, Web, Gadget, Mobile, Automobile, Movie Releated News Articles..</description>
	<lastBuildDate>Sun, 05 Feb 2012 17:28:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Implementing User Defined Exceptions</title>
		<link>http://www.rockingteam.com/2009/12/implementing-user-defined-exceptions/</link>
		<comments>http://www.rockingteam.com/2009/12/implementing-user-defined-exceptions/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 09:03:24 +0000</pubDate>
		<dc:creator>Alex Jose</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Exception Handling]]></category>
		<category><![CDATA[Implementing User Defined Exception]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.rockingteam.com/2009/11/implementing-user-defined-exceptions/</guid>
		<description><![CDATA[In addition to the built-in exceptions, you can create customized exception as per the application requirements. Each application that you create might have specific constraints. For example, in case of a train reservation application, a passenger must specify the destination. Also, a ticket has to be bought for a passenger who is above three years [...]]]></description>
			<content:encoded><![CDATA[<p>In addition to the built-in exceptions, you can create customized exception as per the application requirements. Each application that you create might have specific constraints.   </p>
<p>For example, in case of a train reservation application, a passenger must specify the destination. Also, a ticket has to be bought for a passenger who is above three years of age.   </p>
<p>Similarly, in the case of a banking application, if a customer is below 18 years of age, he is allowed to open a joint account only. Error handling becomes a necessity while developing applications that account for such constraints. The Exception class or its subclasses in Java do not address these application specific constraints. You can create your own exception classes to address these constraints and ensure the integrity of application data.</p>
<h2>Creating and Handling User-defined Exceptions</h2>
<p>The throw, throws, try, catch, and finally keywords are used while implementing <strong><em>userdefined</em></strong> exceptions. The Exception class inherits all the methods provided by the <em><strong>Throwable</strong></em> class. The user-defined Exception class also inherits the methods defined in the <strong><em>Throwable</em></strong> class. The following table lists the methods defined by the <strong><em>Throwable</em></strong> class:</p>
<table border="0" cellspacing="0" cellpadding="2" width="600">
<tbody>
<tr>
<td valign="top" width="300">Methods</td>
<td valign="top" width="300">Description</td>
</tr>
<tr>
<td valign="top" width="300">String getMessage()</td>
<td valign="top" width="300">
<p>Returns a description of the           <br />exception</p>
</td>
</tr>
<tr>
<td valign="top" width="300">String toString()</td>
<td valign="top" width="300">
<p>Returns a string object containing a           <br />description of the exception. This            <br />method is called by the println()            <br />method when throwable object is            <br />passed to it</p>
</td>
</tr>
<tr>
<td valign="top" width="300">Throwable fillInStackTrace()</td>
<td valign="top" width="300">
<p>Returns a Throwable object that           <br />contains a stack trace</p>
</td>
</tr>
<tr>
<td valign="top" width="300">void printStackTrace()</td>
<td valign="top" width="300">Prints the stack trace</td>
</tr>
<tr>
<td valign="top" width="300">
<p>void printStackTrace(PrintStream           <br />stream)</p>
</td>
<td valign="top" width="300">
<p>Propels the stack trace to a specific           <br />defined stream</p>
</td>
</tr>
<tr>
<td valign="top" width="300">
<p>void printStackTrace(PrintWriter           <br />stream)</p>
</td>
<td valign="top" width="300">
<p>Propels the stack trace to a specific           <br />defined stream</p>
</td>
</tr>
<tr>
<td valign="top" width="300">String getLocalizedMessage</td>
<td valign="top" width="300">
<p>Provides a localized description of           <br />the exception.</p>
</td>
</tr>
</tbody>
</table>
<p>&#160;</p>
<p>You can use the following code to create a user-defined exception to catch the exception raised when the sum of two numbers exceeds 20:</p>
<blockquote><p>class UserException extends Exception     <br />{      <br />int num1,num2,sum;      <br />UserException(int a,int b)      <br />{      <br />num1 = a; num2 = b; sum = a + b;}      <br />public String toString()      <br />{      <br />return &quot;UserException Caught: The sum of the numbers Exceeds      <br />20..&quot;; }      <br />}      <br />class UserExceptionDemo      <br />{      <br />static void calculate(int a,int b) throws UserException      <br />{      <br />int sum;      <br />System.out.println(&quot;Calculate Method(&quot; + a + &quot;,&quot; + b +      <br />&quot;)&quot;);      <br />sum=a+b;      <br />if(sum&gt;20)      <br />throw new UserException(a,b);      <br />System.out.println(&quot;The Value of the sum of two numbers      <br />is:&quot; + sum);      <br />}      <br />public static void main(String args[])      <br />{      <br />try      <br />{      <br />calculate(12,1);      <br />calculate(15,7);      <br />}      <br />catch (UserException Obja)      <br />1B.42 Programming in Java      <br />{      <br />System.out.println(&quot;Caught:&quot; + Obja);      <br />}      <br />}      <br />}</p>
</blockquote>
<p>In the preceding code, the <strong><em>UserException</em></strong> subclass of the <strong><em>Exception</em></strong> class is created. The <strong><em>UserException</em></strong> class has one constructor, <strong><em>UserException()</em></strong> and the <strong><em>toString()</em></strong> method that returns a string containing the description of the exception. The <strong><em>UserExceptionDemo</em></strong> class defines the <strong><em>calculate()</em></strong> method that throws an object of the user-defined exception, <em><strong>UserException</strong></em>. The exception is thrown when the sum of two numbers passed as parameters to the <strong><em>calculate()</em></strong> method exceeds 20.    </p>
<p>The output of the preceding code is:</p>
<p><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Creating User-defined Exceptions" border="0" alt="Creating User-defined Exceptions" src="http://www.rockingteam.com/wp-content/uploads/2009/11/CreatingUserdefinedExceptions.png" width="672" height="167" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rockingteam.com/2009/12/implementing-user-defined-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Tutorial : Exception-Handling &#8211; Summary</title>
		<link>http://www.rockingteam.com/2009/11/java-tutorial-exception-handling-summary/</link>
		<comments>http://www.rockingteam.com/2009/11/java-tutorial-exception-handling-summary/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 06:41:29 +0000</pubDate>
		<dc:creator>Alex Jose</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Assertion In Java]]></category>
		<category><![CDATA[Built-in Exceptions]]></category>
		<category><![CDATA[Exception Classes]]></category>
		<category><![CDATA[Exception Handling]]></category>
		<category><![CDATA[Implenting Exception Handling]]></category>
		<category><![CDATA[Thrwing An Exception]]></category>
		<category><![CDATA[User Defined Exception]]></category>

		<guid isPermaLink="false">http://www.rockingteam.com/?p=2683</guid>
		<description><![CDATA[Errors can be broadly categorized into two groups on the basis of whether the compiler is able to handle the error or not, such as compile time errors and runtime errors. An exception is a run-time error that can be defined as an abnormal event that occurs during the execution of a program and disrupts [...]]]></description>
			<content:encoded><![CDATA[<ul>
<li>Errors can be broadly categorized into two groups on the basis of whether the compiler is able to handle the error or not, such as compile time errors and runtime errors.</li>
<li>An exception is a run-time error that can be defined as an abnormal event that occurs during the execution of a program and disrupts the normal flow of instructions.</li>
<li>In Java, the Throwable class is the superclassof all the exception classes. It is the class at the top position in the exception class hierarchy. The Exception class and the Error class are two direct subclasses of the Throwable class.</li>
<li>The built-in exceptions in Java are divided into two types on the basis of the conditions where the exception is raised:
<ul>
<li>Checked Exceptions or Compiler-enforced Exceptions</li>
<li>Unchecked exceptions or Runtime Exceptions</li>
</ul>
</li>
<li>You can implement exception-handling in your program by using the following keywords:
<ul>
<li>try</li>
<li>catch</li>
<li>throw</li>
<li>throws</li>
<li>finally</li>
</ul>
</li>
<li>You use multiple catch blocks to throw more than one type of exception.</li>
<li>The finally clause is used to execute the statements that need to be executed whether or not an exception has been thrown.</li>
<li>The throw statement causes termination of the normal flow of control of the</li>
<li>Java code and stops the execution of subsequent statements after the throw statement.</li>
<li>The throws clause is used by a method to specify the types of exceptions the method throws.</li>
<li>You can create your own exception classes to handle the situations specific to an application.</li>
<li>You can use assertions to check and ensure that any assumption made at the start of a program is true throughout the program.</li>
</ul>
<h2>Exception-Handling related Tutorial Article</h2>
<ol>
<li><a title="Java Tutorial : Exception Handling" href="http://www.rockingteam.com/2009/09/java-tutorial-exception-handling/">Java Tutorial : Exception Handling</a></li>
<li><a title="Java Tutorial : Exception Classes" href="http://www.rockingteam.com/2009/10/java-tutorial-exception-classes/">Java Tutorial : Exception Classes</a></li>
<li><a title="Java Tutorial : Built-in Exceptions" href="http://www.rockingteam.com/2009/10/java-tutorial-built-in-exceptions/">Java Tutorial : Built-in Exceptions</a></li>
<li><a title="Java Tutorial : Implementing Exception Handling" href="http://www.rockingteam.com/2009/10/java-tutorial-built-in-exceptions/">Java Tutorial : Implementing Exception Handling</a></li>
<li><a title="Java Tutorial : Implementing Exception Handling – Part 2" href="http://www.rockingteam.com/2009/10/java-tutorial-implementing-exception-handling-part-2/">Java Tutorial : Implementing Exception Handling – Part 2</a></li>
<li><a title=" 	Java Tutorial : Throwing An Exception – Part 1" href="http://www.rockingteam.com/2009/10/java-tutorial-throwing-an-exception-part-1/">Java Tutorial : Throwing An Exception – Part 1</a></li>
<li><a title="Java Tutorial : Throwing An Exception – Part 2" href="http://www.rockingteam.com/2009/10/java-tutorial-throwing-an-exception-%e2%80%93-part-2/">Java Tutorial : Throwing An Exception – Part 2</a></li>
<li><a title="Implementing User Defined Exceptions" href="http://www.rockingteam.com/2009/11/implementing-user-defined-exceptions/">Implementing User Defined Exceptions</a></li>
<li><a title="Java Tutorial : Assertion In Java" href="http://www.rockingteam.com/2009/11/java-tutorial-assertion-in-java/">Java Tutorial : Assertion In Java</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.rockingteam.com/2009/11/java-tutorial-exception-handling-summary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Tutorial : Throwing An Exception – Part 2</title>
		<link>http://www.rockingteam.com/2009/10/java-tutorial-throwing-an-exception-%e2%80%93-part-2/</link>
		<comments>http://www.rockingteam.com/2009/10/java-tutorial-throwing-an-exception-%e2%80%93-part-2/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 11:15:21 +0000</pubDate>
		<dc:creator>Alex Jose</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Displaying the Description of an Exception]]></category>
		<category><![CDATA[Exception Handling]]></category>
		<category><![CDATA[Java Tutorial]]></category>
		<category><![CDATA[Throwing An Exception]]></category>
		<category><![CDATA[Using Throw Statement]]></category>

		<guid isPermaLink="false">http://www.rockingteam.com/?p=2634</guid>
		<description><![CDATA[Java Tutorial : Throwing An Exception – Part 1 Printing a Stack Trace When an exception is caught, you can find out the method or the line of the code where the exception is raised by using the printStackTrace() method. You can call the printStackTrace() method for printing the stack trace. The objects of the [...]]]></description>
			<content:encoded><![CDATA[<blockquote>
<ul>
<li> <strong><a rel="bookmark" href="http://www.rockingteam.com/2009/10/java-tutorial-throwing-an-exception-part-1/">Java Tutorial : Throwing An Exception – Part 1</a></strong></li>
</ul>
</blockquote>
<h2>Printing a Stack Trace</h2>
<p>When an exception is caught, you can find out the method or the line of the code where the exception is raised by using the <em><strong>printStackTrace()</strong></em> method. You can call the <em><strong>printStackTrace()</strong></em> method for printing the stack trace. The objects of the Throwable class inherit the stack trace. You can use the following code to print the stack trace for the <em><strong>ArithmeticException </strong></em>exception:</p>
<blockquote><p>class PrintStack<br />
{<br />
public static void main(String args[])<br />
{<br />
int Num1= 30 , Num2 = 0;<br />
try<br />
{<br />
int Num3=Num1/Num2;<br />
}<br />
catch(ArithmeticException obj)<br />
{<br />
obj.printStackTrace();<br />
}<br />
}<br />
}</p></blockquote>
<p>In the preceding code, <em><strong>printStackTrace()</strong></em> method is used to print the stack trace to check the line in the code where the exception is raised.</p>
<div id="attachment_2637" class="wp-caption aligncenter" style="width: 678px"><a href="http://www.rockingteam.com/wp-content/uploads/2009/10/Using-the-printStackTrace-Statement.png" rel="lightbox[2634]"><img class="size-full wp-image-2637" title="Using the printStackTrace Statement" src="http://www.rockingteam.com/wp-content/uploads/2009/10/Using-the-printStackTrace-Statement.png" alt="Using the printStackTrace Statement" width="668" height="163" /></a><p class="wp-caption-text">Using the printStackTrace Statement</p></div>
<p style="text-align: center;">The output of the preceding code is:</p>
<h2>Rethrowing an Exception</h2>
<p>A catch block can rethrow an exception without handling it by using the throw statement. Exceptions are rethrown if a method causing the error wants to pass the error handling responsibilities to the exception-handlers in some other methods. You can use the following code snippet to rethrow an exception:</p>
<blockquote><p>catch(Exception e)<br />
{<br />
System.out.println(&#8220;Exception Raised&#8221;);<br />
throw e;<br />
}</p></blockquote>
<p>The exception goes to the catch block of the next higher context ignoring the catch blocks of the same try block.</p>
<p>You can use the following code to catch the <em><strong>NullPointerException </strong></em>and <em><strong>rethrow </strong></em>the exception to the outer handler:</p>
<blockquote><p>class RethrowException<br />
{<br />
static void throwDemo()<br />
{<br />
try<br />
{<br />
throw new NullPointerException (&#8220;My Exception&#8221;);<br />
}<br />
catch(NullPointerException e)<br />
{<br />
System.out.println(&#8220;Exception caught in throwDemo()<br />
method&#8221;);<br />
throw e; // Rethrow the Exception.<br />
}<br />
}<br />
public static void main(String args[])<br />
{<br />
try<br />
{<br />
throwDemo();<br />
}<br />
catch(NullPointerException e)<br />
{<br />
System.out.println(&#8220;Exception caught:&#8221; + e);<br />
}<br />
}<br />
}</p></blockquote>
<p>In the preceding code, the catch block in the <em><strong>throwDemo()</strong></em> method rethrows the <em><strong>NullPointer </strong></em>exception to the catch block defined in the main() method. The output of the preceding code is:</p>
<div id="attachment_2636" class="wp-caption aligncenter" style="width: 678px"><a href="http://www.rockingteam.com/wp-content/uploads/2009/10/Rethrowing-an-Exception.png" rel="lightbox[2634]"><img class="size-full wp-image-2636" title="Rethrowing an Exception" src="http://www.rockingteam.com/wp-content/uploads/2009/10/Rethrowing-an-Exception.png" alt="Rethrowing an Exception" width="668" height="163" /></a><p class="wp-caption-text">Rethrowing an Exception</p></div>
<h2>Using the throws Statement</h2>
<p>The <em><strong>throws </strong></em>statement is used by a method to specify the types of exceptions the method throws. If a method is capable of raising an exception that it does not handle, the method must specify that the exception have to be handled by the calling method. This is done using the throws statement. The throws clause lists the types of exceptions that a method might throw. The following syntax shows how to declare a method that specifies a throws clause:<br />
<em><strong>[&lt;access_specifier&gt;] [&lt;modifier&gt;] &lt;return_type&gt; &lt;method_name&gt;<br />
[&lt;arg_list&gt;] [throws &lt;exception_list&gt;]</strong></em><br />
You can use the following code to throw the <em><strong>ClassNotFoundException </strong></em>exception:</p>
<blockquote><p>class NoClassexception<br />
{<br />
static void throwMethod()<br />
{<br />
System.out.println(&#8220;In throwMethod&#8221;);<br />
throw new ClassNotFoundException();<br />
}<br />
public static void main(String args[])<br />
{<br />
throwMethod();<br />
// no try and catch statements to handle the exception<br />
}<br />
}</p></blockquote>
<p>In the preceding code, the code does not compile because the caller method throws an exception but does not catch the exception. You can compile the code by first declaring that the <em><strong>throwMethod()</strong></em> throws the <em><strong>ClassNotFoundException </strong></em>exception. In addition, the <em><strong>main()</strong></em> method must define the try-catch statements to catch the exception. You can use the following code to use the throws statement:</p>
<blockquote><p>class ThrowsClass<br />
{<br />
static void throwMethod() throws ClassNotFoundException<br />
{<br />
System.out.println(&#8220;In throwMethod&#8221;);<br />
throw new ClassNotFoundException();<br />
}<br />
public static void main(String args[])<br />
{<br />
try<br />
{<br />
throwMethod();<br />
}<br />
catch(ClassNotFoundException Obja)<br />
{<br />
System.out.println(&#8220;throwMethod has thrown an<br />
Exception:&#8221; + Obja);<br />
}<br />
}<br />
}</p></blockquote>
<p>The output of the preceding code is:</p>
<div id="attachment_2638" class="wp-caption aligncenter" style="width: 678px"><a href="http://www.rockingteam.com/wp-content/uploads/2009/10/Using-the-Throws-Statement.png" rel="lightbox[2634]"><img class="size-full wp-image-2638" title="Using the Throws Statement" src="http://www.rockingteam.com/wp-content/uploads/2009/10/Using-the-Throws-Statement.png" alt="Using the Throws Statement" width="668" height="163" /></a><p class="wp-caption-text">Using the Throws Statement</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.rockingteam.com/2009/10/java-tutorial-throwing-an-exception-%e2%80%93-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Java Tutorial : Throwing An Exception &#8211; Part 1</title>
		<link>http://www.rockingteam.com/2009/10/java-tutorial-throwing-an-exception-part-1/</link>
		<comments>http://www.rockingteam.com/2009/10/java-tutorial-throwing-an-exception-part-1/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 10:27:04 +0000</pubDate>
		<dc:creator>Alex Jose</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Displaying the Description of an Exception]]></category>
		<category><![CDATA[Exception Handling]]></category>
		<category><![CDATA[Java Tutorial]]></category>
		<category><![CDATA[Throwing An Exception]]></category>
		<category><![CDATA[Using Throw Statement]]></category>

		<guid isPermaLink="false">http://www.rockingteam.com/?p=2590</guid>
		<description><![CDATA[You can throw an exception explicitly using the throw statement. For example, you need to throw an exception when a user enters a wrong loginID or password. The throws clause is used to list the types of exceptions that can be thrown during the execution of a method in a program. Using the throw Statement [...]]]></description>
			<content:encoded><![CDATA[<p>You can throw an exception explicitly using the <em><strong>throw</strong></em> statement. For example, you need to throw an exception when a user enters a wrong loginID or password. The throws clause is used to list the types of exceptions that can be thrown during the execution of a method in a program.</p>
<h2>Using the throw Statement</h2>
<p>The throw statement causes termination of the normal flow of control of the Java code and stops the execution of the subsequent statements if an exception is thrown when the throw statement is executed. The throw clause transfers the control to the nearest catch block handling the type of exception object throws. If no such catch block exists, the program terminates. The throw statement accepts a single argument, which is an object of the Exception class.</p>
<p>The following syntax shows how to declare the throw statement:</p>
<p><em><strong>throw ThrowableObj</strong></em></p>
<p>In the preceding syntax, <em><strong>ThrowableObj </strong></em>is an object of the Throwable class or a subclass of the Throwable class, such as Exception class. The <em><strong>ThrowableObj </strong></em>object is created using the new operator. The Java compiler gives an error if the <em><strong>ThrowableObj </strong></em>object does not belong to a valid Exception class. You can use the following code to throw the <em><strong>IllegalStateException </strong></em>exception:</p>
<blockquote><p>
class ThrowStatement<br />
{<br />
static void throwDemo()<br />
{<br />
try<br />
{<br />
throw new IllegalStateException();<br />
// creating and throwing an object of the<br />
IllegalStateException class.<br />
}<br />
catch(NullPointerException objA)<br />
{<br />
System.out.println(&#8220;Not caught by catch block inside<br />
throwDemo().&#8221;);<br />
}<br />
}<br />
public static void main(String args[])<br />
{<br />
try<br />
{<br />
throwDemo();<br />
}<br />
catch(IllegalStateException objB)<br />
{<br />
// catching an object of the<br />
IllegalStateException class.<br />
System.out.println(&#8221; Exception Caught in :&#8221; + objB);<br />
}<br />
}<br />
}</p></blockquote>
<p>In the preceding code, the new operator is used to construct an object of <em><strong>IllegalStateException</strong></em>. In the <em><strong>throwDemo()</strong></em> method , an exception of the type <em><strong>IllegalStateException </strong></em>is thrown.</p>
<p>The output of the preceding code is:</p>
<div id="attachment_2592" class="wp-caption aligncenter" style="width: 678px"><a href="http://www.rockingteam.com/wp-content/uploads/2009/10/Using-the-Throw-Statement.png" rel="lightbox[2590]"><img class="size-full wp-image-2592" title="Using the Throw Statement" src="http://www.rockingteam.com/wp-content/uploads/2009/10/Using-the-Throw-Statement.png" alt="Using the Throw Statement" width="668" height="163" /></a><p class="wp-caption-text">Using the Throw Statement</p></div>
<p style="text-align: center;">
<h2>Displaying the Description of an Exception</h2>
<p>The built-in exception classes in Java have two types of constructors: one with no parameter and another with a String parameter that defines the exception. The String parameter that you pass to the constructor of an exception can be displayed when exception object is passed as an argument to the <em><strong>System.out.println()</strong></em> statement. The following code shows the use of constructor of the <em><strong>IlegalStateException</strong></em> with String parameter:</p>
<blockquote><p>
class ThrowClass<br />
{<br />
static void throwDemo()<br />
{<br />
try<br />
{<br />
throw new IllegalStateException(&#8220;MyException&#8221;);<br />
}<br />
catch(IllegalStateException objA)<br />
{<br />
System.out.println(&#8220;Caught:&#8221; + objA);<br />
}<br />
} // End of throwDemo() method.<br />
public static void main(String args[])<br />
{<br />
throwDemo();<br />
}<br />
}// End of the ThrowClass.</p></blockquote>
<p>In the preceding code, description added to the constructor of the IllegalStateException exception is displayed as MyException.</p>
<p>The output of the preceding code is:</p>
<div id="attachment_2591" class="wp-caption aligncenter" style="width: 678px"><a href="http://www.rockingteam.com/wp-content/uploads/2009/10/Displaying-the-Description-of-an-Exception.png" rel="lightbox[2590]"><img class="size-full wp-image-2591" title="Displaying the Description of an Exception" src="http://www.rockingteam.com/wp-content/uploads/2009/10/Displaying-the-Description-of-an-Exception.png" alt="Displaying the Description of an Exception" width="668" height="163" /></a><p class="wp-caption-text">Displaying the Description of an Exception</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.rockingteam.com/2009/10/java-tutorial-throwing-an-exception-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Java Tutorial : Implementing Exception Handling &#8211; Part 2</title>
		<link>http://www.rockingteam.com/2009/10/java-tutorial-implementing-exception-handling-part-2/</link>
		<comments>http://www.rockingteam.com/2009/10/java-tutorial-implementing-exception-handling-part-2/#comments</comments>
		<pubDate>Sun, 18 Oct 2009 06:41:51 +0000</pubDate>
		<dc:creator>Alex Jose</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Exception Handling]]></category>
		<category><![CDATA[Handling the Unreachable Code]]></category>
		<category><![CDATA[Java Tutorial]]></category>
		<category><![CDATA[Using the finally Clause]]></category>

		<guid isPermaLink="false">http://www.rockingteam.com/?p=2578</guid>
		<description><![CDATA[Handling the Unreachable Code Problem The multiple catch blocks generate unreachable code error. If the first catch block contains the Exception class object then the subsequent catch blocks are never executed. The Exception class being the superclass of all the exception classes catches various types of exceptions. The Java compiler gives an error stating that [...]]]></description>
			<content:encoded><![CDATA[<h3>Handling the Unreachable Code Problem</h3>
<p>The <a title="Using Multiple Catch Blocks" href="http://www.rockingteam.com/2009/10/java-tutorial-implementing-exception-handling/">multiple catch blocks</a> generate unreachable code error. If the first catch block contains the Exception class object then the subsequent catch blocks are never executed. The Exception class being the superclass of all the exception classes catches various types of exceptions. The Java compiler gives an error stating that the subsequent catch blocks have not been reached. This is known as the unreachable code problem. To avoid unreachable code error, the last catch block in multiple catch blocks must contain the Exception class object.<br />
The following code is an example of the unreachable code problem:</p>
<blockquote><p>class UnreachableCodeDemo<br />
{<br />
public static void main(String args[])<br />
{<br />
int num1=5, num2=0, num3;<br />
try<br />
{<br />
num3=num1/num2;<br />
}<br />
catch(Exception e)<br />
{<br />
System.out.println(&#8220;Error&#8221;);<br />
}<br />
catch(ArithmeticException e)<br />
{<br />
System.out.println(&#8220;Division by zero.&#8221;);<br />
//Unreachable catch statement.<br />
}<br />
}<br />
}</p></blockquote>
<p>In the preceding code, the Java compiler gives an error stating that the catch block handling ArithmeticException has not been reached. This is because the Exception class, being the base class for all exceptions, handles all the exceptions that are raised by the Java code in the very first catch block. The output of the preceding code is:</p>
<div id="attachment_2579" class="wp-caption aligncenter" style="width: 678px"><a href="http://www.rockingteam.com/wp-content/uploads/2009/10/Unreachable-Code-Problem.png" rel="lightbox[2578]"><img class="size-full wp-image-2579" title="Unreachable Code Problem" src="http://www.rockingteam.com/wp-content/uploads/2009/10/Unreachable-Code-Problem.png" alt="Unreachable Code Problem" width="668" height="163" /></a><p class="wp-caption-text">Unreachable Code Problem</p></div>
<p>The unreachable code problem can be solved if the subclasses, such as ArithmeticException, ArrayIndexOutOfBounds, or NullPointerException in the multiple catch statements are written before the Exception superclass.</p>
<h3>Using the finally Clause</h3>
<p>When an <a title="Exception Handling" href="http://www.rockingteam.com/tag/exception-handling/">exception</a> is raised, the statements in the try block written after the statement in which the exception occurred are ignored. The finally block is used to process certain statements, no matter whether an exception is raised or not. The block of code attached with the finally clause is executed after a try-catch block has been executed. The following syntax shows how to declare the try-finally block:</p>
<blockquote><p>try<br />
{<br />
// Block of code<br />
}<br />
finally<br />
{<br />
// Block of code that is always executed irrespective of an<br />
exception being raised or not.<br />
}</p></blockquote>
<p>If there is a catch block associated with the try block, the finally clause is written after the catch block. The following syntax shows how to declare the try-catch-finally block:</p>
<blockquote><p>
try<br />
{<br />
// Block of code.<br />
}<br />
catch(execption1 obj1)<br />
{<br />
System.out.println(&#8220;Exception1 has been raised&#8221;);<br />
}<br />
catch(exception2 obj2)<br />
{<br />
System.out.println(&#8220;Exception2 has been raised&#8221;);<br />
}<br />
finally<br />
{<br />
// Block of code that is always executed irrespective of an<br />
exception being raised or not.<br />
}</p></blockquote>
<p>The finally block executes whether or not an exception is raised. If an exception is thrown, the finally block executes even if no catch statement matches the exception. For example, a file has to be closed irrespective of whether an exception is raised or not. You can place the code to close the file in both the try and catch blocks.</p>
<p>To avoid duplication of code, you can place the code in the finally block. The following code snippet is used to write a try-catch block to declare methods to open and write to a file:</p>
<blockquote><p>
try<br />
{<br />
openFile();<br />
writeFile(); // Might raise an exception.<br />
}<br />
catch(ExceptionName obj)<br />
{<br />
// Process the exception.<br />
}<br />
finally<br />
{<br />
closeFile();<br />
}</p></blockquote>
<p>In the preceding code, the finally clause is executed regardless of whether an exception is thrown or not. You can have only one finally block for an exceptionhandler. You can use the finally clause to execute the closeFile() method.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rockingteam.com/2009/10/java-tutorial-implementing-exception-handling-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

