<?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; Java</title>
	<atom:link href="http://www.rockingteam.com/tag/java/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>Java Tutorial : The super and this Keywords</title>
		<link>http://www.rockingteam.com/2010/03/java-tutorial-the-super-and-this-keywords/</link>
		<comments>http://www.rockingteam.com/2010/03/java-tutorial-the-super-and-this-keywords/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 17:44:24 +0000</pubDate>
		<dc:creator>Alex Jose</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Tutorial]]></category>
		<category><![CDATA[Keyword]]></category>
		<category><![CDATA[Super]]></category>
		<category><![CDATA[This]]></category>

		<guid isPermaLink="false">http://www.rockingteam.com/?p=2782</guid>
		<description><![CDATA[Java provides the super keyword that enables a subclass to refer to its superclass. The super keyword is used to access: superclass constructors superclass methods and variables Consider a Java application, which consists of multiple classes. The subclass in the application needs to reuse the constructor of the superclass. In this application, super keyword is used as [...]]]></description>
			<content:encoded><![CDATA[<p>Java provides the super keyword that enables a subclass to refer to its superclass. The super keyword is used to access:</p>
<ul>
<li>superclass constructors</li>
<li>superclass methods and variables</li>
</ul>
<p>Consider a Java application, which consists of multiple classes. The subclass in the application needs to reuse the constructor of the superclass. In this application, super keyword is used as a method and invokes the constructor of its immediate superclass. The syntax to invoke the constructor of a superclass using the super() method is:</p>
<blockquote><p>super (&lt;parameter1&gt;, &lt;parameter 2&gt;,..,&lt;parameterN&gt;);</p></blockquote>
<p>In the above syntax, &lt;parameter1&gt;, &lt;parameter 2&gt;,..,&lt;parameterN&gt; refer to the list of parameters that you need to pass to the constructor of the superclass.</p>
<p>If no parameters are passed to the super() method, it invokes the default constructor of the superclass. If the superclass contains the overloaded constructor, you can pass parameters to the super() method to invoke a particular constructor. When you use the super() method in the constructor of the subclass, it should be the first executable statement in the constructor.</p>
<p>Consider a Java application, in which both, superclass and subclass, have member variables with the same name. In this application, the super keyword is used to access the member variables of the superclass. The syntax to access the member variable of a superclass is:</p>
<blockquote><p>super.&lt;variable&gt;;</p></blockquote>
<p>The subclass can also access the member methods of the superclass using the super keyword. The following code snippet shows the Literature subclass accessing the member variable of the Book superclass:</p>
<blockquote><p>class Book<br />
{<br />
int categoryCode;<br />
}<br />
class Literature extends Book<br />
{<br />
int categoryCode;<br />
public void input(int c1, int c2)<br />
{<br />
super.categoryCode=c1;<br />
categoryCode=c2;<br />
}<br />
}</p></blockquote>
<p>In the above code snippet, the Book class contains the categoryCode variable to store the category of a book, such as literature, science, fiction, suspense, and history. The Literature class also contains the categoryCode variable to store the subcategory of a book. For example for literature category, the subcategories are English, French, Hindi, and Russian. The input() method accepts two values for c1 and c2 variables. The value that you pass for c1 variable is assigned to the superclass variable whereas the value of the c2 variable is assigned to the subclass variable. The this keyword is used to refer to the current object. You can use the this keyword when a method defined in a Java class needs to refer to the object used to invoke that method, as shown in the following code snippet:</p>
<blockquote><p>Book(int bcode, double bprice)<br />
{<br />
this.bookCode=bcode;<br />
this.bookPrice=bprice;<br />
}</p></blockquote>
<p>In the above code snippet, the this keyword refers to the object that invokes the Book() constructor.</p>
<p>Another situation where you can use the this keyword is when the local and instance variables have the same name. For example, the Book class has two member variables, bookCode and bookPrice. The constructor of the Book class is used to initialize its member. If the formal parameters passed to the constructor have the same name as instance variables, you need to use the this keyword to refer to the<br />
instance variables. The following code snippet shows how to use the this keyword when instance and formal parameters have the same name:</p>
<blockquote><p>Book(int bcode, double bprice)<br />
{<br />
this.bcode=bcode;<br />
this.bprice=bprice;<br />
}</p></blockquote>
<p>In the above code snippet, this keyword refers to the instance variables, bcode and bprice. The values of the formal parameters, bcode and bprice of the Book() constructor are assigned to the instance variables.</p>
<p>In addition, you can use the this keyword in a constructor of a class to invoke another constructor of the class. Unlike the super keyword, the this keyword can invoke the constructor of the same class. The following code snippet shows how to invoke a constructor using the this keyword:</p>
<blockquote><p>class Book<br />
{<br />
public Book(String bname)<br />
{<br />
this(bname, 1001);<br />
}<br />
public Book(String bname, int bcode)<br />
{<br />
bookName=bname;<br />
bookCode=bcode;<br />
}<br />
}</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.rockingteam.com/2010/03/java-tutorial-the-super-and-this-keywords/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Tutorial : Implementing Interfaces &#8211; Example</title>
		<link>http://www.rockingteam.com/2010/01/java-tutorial-implementing-interfaces-example/</link>
		<comments>http://www.rockingteam.com/2010/01/java-tutorial-implementing-interfaces-example/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 07:00:00 +0000</pubDate>
		<dc:creator>Alex Jose</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[Implementing Interfaces in Java]]></category>
		<category><![CDATA[Interfaces In Java]]></category>
		<category><![CDATA[Java Tutorial]]></category>

		<guid isPermaLink="false">http://www.rockingteam.com/?p=2511</guid>
		<description><![CDATA[David Johnson is the owner of Safest Software Co. that deals in buying and selling of hardware and software computer products. Don Allen, an employee of the Safest Software Co. is assigned the task to develop a Java application to keep track of the inventory of the computer hardware and software products. The application consists [...]]]></description>
			<content:encoded><![CDATA[<p>David Johnson is the owner of Safest Software Co. that deals in buying and selling of hardware and software computer products. Don Allen, an employee of the Safest Software Co. is assigned the task to develop a Java application to keep track of the inventory of the computer hardware and software products.</p>
<p>The application consists of an interface, <em><strong>ItemCount</strong></em> that includes the declaration of methods to be used in the superclass and subclass. The application includes the <em><strong>SoftwareProducts</strong></em> class that accepts the number of software items and calculates the<br />
number.</p>
<p>The <em><strong>HardwareProducts</strong></em> class is included to accept the number of hardware items and to calculate the number. The <em><strong>Products</strong></em> class is included to create objects for the <em><strong>SoftwareProducts</strong></em> and <em><strong>HardwareProducts</strong></em> classes.<br />
You can use the following code to create the <em><strong>ItemCount</strong></em> interface in Java:</p>
<blockquote><p>interface ItemCount //Defining the interface.<br />
{<br />
//Defining abstract methods in the Billing interface.<br />
public void noOfItems();<br />
}</p></blockquote>
<p>In the preceding code, the <em><strong>ItemCount</strong></em> interface contains the <em><strong>noOfItems()</strong></em> abstract method that needs to be implemented in the classes that implement the<em><strong> ItemCount</strong></em> interface.<br />
You can use the following code to create the <em><strong>SoftwareProducts</strong></em> superclass:</p>
<blockquote><p>class SoftwareProducts implements ItemCount<br />
{<br />
// Defining the data members.<br />
int os= 60;<br />
int oracle = 20;<br />
int jav = 15;<br />
int tot;<br />
//Defining the methods.<br />
public void calculate()<br />
{<br />
tot = os + oracle + jav;<br />
}<br />
public void noOfItems() // Method declared publicly in the interface.<br />
{<br />
System.out.println();<br />
System.out.println(&#8221; &#8220;+&#8221;Total number of software products purchased is :<br />
&#8220;+ tot);}<br />
}</p></blockquote>
<p>In the preceding code, the <em><strong>SoftwareProducts</strong></em> superclass defines the data members, <em><strong>os</strong></em>, <em><strong>oracle</strong></em>, and <em><strong>jav</strong></em> and the methods, <em><strong>calculate()</strong></em> and <em><strong>noOfItems()</strong></em>. These data members store the number of software products, Operating System, Oracle, and Java. The <em><strong>noOfItems()</strong></em> method displays the total number of the software items purchased by David.<br />
You can use the following code to create the HardwareProducts class:</p>
<blockquote><p>class HardwareProducts implements ItemCount<br />
{<br />
// Defining the data members.<br />
int printer = 20;<br />
int speaker = 12;<br />
int harddisk = 25;<br />
int htot;<br />
public void calculate1()<br />
{<br />
htot = printer + speaker + harddisk;<br />
}<br />
public void noOfItems()// The method is declared publicly in the<br />
interface, Billing.<br />
{<br />
System.out.println();<br />
System.out.println(&#8221; &#8220;+&#8221;Total number of hardware items purchased is : &#8221;<br />
+htot);<br />
}}<br />
class Products<br />
{<br />
public static void main(String args[])<br />
{<br />
//Creating an object of the class, Hardware.<br />
SoftwareProducts s = new SoftwareProducts();<br />
HardwareProducts h = new HardwareProducts();<br />
s.calculate();<br />
h.calculate1();<br />
s.noOfItems();<br />
h.noOfItems();<br />
}}</p></blockquote>
<p>In the preceding code, the HardwareProducts subclass implements the <em><strong>ItemCount</strong></em> interface. The<em><strong> HardwareProducts</strong></em> class defines the data members, such as printer, speaker, and hard disk. These data members store the number of printer, speaker, and hard disk. The HardwareProducts class defines the <em><strong>calculate1()</strong></em> method to calculate the total number of hardware items. The <em><strong>noOfItems()</strong></em> method displays the total number of hardware items purchased by David.<br />
The following figure shows the output of the preceding code:</p>
<div class="wp-caption aligncenter" style="width: 678px"><a href="../wp-content/uploads/2009/09/implementing-inheritance-example.png" rel="lightbox[2511]"><img title="implementing inheritance example" src="../wp-content/uploads/2009/09/implementing-inheritance-example.png" alt="implementing inheritance example" width="668" height="186" /></a><p class="wp-caption-text">Using Interface to Display the Number of Items Purchased</p></div>
<p style="text-align: center;">
<p>Interfaces also enable you to declare constants that can be imported into multiple classes. The constant values declared in an interface can be implemented in any class. The constants defined in an interface are declared using the <em><strong>final</strong></em> keyword. You can use the following code to create the TBooks interface in Java:</p>
<blockquote><p>interface TBooks<br />
{<br />
//Defining constants in an interface.<br />
final int cd =10;<br />
final int book =5;<br />
}</p></blockquote>
<p>In the preceding code, the TBooks interface declares a set of constants that stores the number of books and Compact Discs (CD) purchased.<br />
You can use the following code to create the Dcd class:</p>
<blockquote><p>class Dcd implements TBooks<br />
{<br />
void display()<br />
{<br />
System.out.println(&#8221; &#8220;);<br />
System.out.println(&#8220;\t The number of purchased CD&#8217;s are :&#8221; + cd);<br />
System.out.println(&#8221; &#8220;);<br />
}<br />
}<br />
In the preceding code, the <em><strong>Dcd</strong></em> class implements the <em><strong>TBooks</strong></em> interface to display the<br />
number of purchased CDs.<br />
You can use the following code to create the <em><strong>DBooks</strong></em> class:<br />
class DBooks extends Dcd implements TBooks<br />
{<br />
void display1()<br />
{<br />
System.out.println(&#8220;\t The number of purchased books are :&#8221; + book);<br />
System.out.println(&#8220;\t The number of purchased CD&#8217;s are :&#8221; + cd);<br />
System.out.println(&#8221; &#8220;);<br />
}<br />
public static void main (String args[])<br />
{<br />
// Creating an instance of DBooks class.<br />
DBooks db = new DBooks();<br />
TBooks t;<br />
t = db;<br />
db.display1();<br />
}}</p></blockquote>
<p>In the preceding code, the <em><strong>DBooks</strong></em> subclass extends from the <em><strong>Dcd</strong></em> superclass and implements the <em><strong>TBooks</strong></em> interface. The <em><strong>display1() </strong></em>method displays the number of purchased books and CDs using inheritance.<br />
The following figure shows the output of the preceding code:</p>
<div id="attachment_2512" class="wp-caption aligncenter" style="width: 678px"><a href="http://www.rockingteam.com/wp-content/uploads/2009/09/implementing-inheritance-example-2.png" rel="lightbox[2511]"><img class="size-full wp-image-2512" title="implementing inheritance example 2" src="http://www.rockingteam.com/wp-content/uploads/2009/09/implementing-inheritance-example-2.png" alt="implementing inheritance example 2" width="668" height="235" /></a><p class="wp-caption-text">Number of Purchased CDs and Books</p></div>
<p style="text-align: center;">
]]></content:encoded>
			<wfw:commentRss>http://www.rockingteam.com/2010/01/java-tutorial-implementing-interfaces-example/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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 : 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>
	</channel>
</rss>

