<?xml version="1.0" encoding="ISO-8859-1"?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<title>Java Programming Forums - The Java Community - Blogs - newbie</title>
		<link>http://www.javaprogrammingforums.com/blogs/newbie/</link>
		<description>The Java Programming Forums are a community of Java programmers from all around the World. Join us now to solve all your Java problems!</description>
		<language>en</language>
		<lastBuildDate>Thu, 23 May 2013 01:22:50 GMT</lastBuildDate>
		<generator>vBulletin</generator>
		<ttl>60</ttl>
		<image>
			<url>http://www.javaprogrammingforums.com/images/misc/rss.jpg</url>
			<title>Java Programming Forums - The Java Community - Blogs - newbie</title>
			<link>http://www.javaprogrammingforums.com/blogs/newbie/</link>
		</image>
		<item>
			<title>Create your own Exceptions!</title>
			<link>http://www.javaprogrammingforums.com/blogs/newbie/30-create-your-own-exceptions.html</link>
			<pubDate>Mon, 09 Jan 2012 18:26:39 GMT</pubDate>
			<description><![CDATA[I was surprised to notice that there wasn't a single tutorial on this forum which covered the creation of your own Exception classes, so I decided to...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">I was surprised to notice that there wasn't a single tutorial on this forum which covered the creation of your own Exception classes, so I decided to make a small tutorial for you.<br />
TL;DR - If you don't fancy reading all the details, you may jump straight to the code, as it's pretty self explanatory if you have a basic concept of Exceptions and their hierarchy.<br />
<br />
<u><b><span style="font-family:  Lucida Sans Unicode"><font size="4">Introduction</font></span></b></u><br />
<br />
You might be surprised to know that creating your own Exception is as easy, if not easier than any other occasion where you would extend a super class.<br />
<br />
The steps are simple:<br />
<div style="margin-left:40px"><font color="#0000FF">1.)</font> Realise a situation where creating your own Exception would be a benefit<br />
<font color="#0000FF">2.)</font> Name your Class the name you want your Exception to be known as<br />
<font color="#0000FF">3.)</font> Make a decision on what Exception to subclass, checked or unchecked (<b>explained below</b>)<br />
<font color="#0000FF">4.)</font> Throw it!</div><br />
<br />
As some, but not all of you may know; there are two types of Exceptions, checked and unchecked where (in short) the differences between the two are that the compiler forces the programmer to handle checked Exceptions, but not unchecked Exceptions.<br />
<br />
Anything which is known as unchecked comes under <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/RuntimeException.html" target="_blank">RuntimeException</a>, and its subclasses.<br />
<br />
Here are a few of the most common Runtime exceptions (See API for full list):<br />
<div style="margin-left:40px"><b><font color="#800080">ArithmeticException, ClassCastException,  IllegalArgumentException, NoSuchElementException, NullPointerException</font></b></div><br />
<br />
All other subclasses of the Exception class (inclusive) are checked and you are therefore forced by the compiler to handle them.<br />
A common example of this scenario is where the compiler forces you to handle IOException when dealing with files and the like.<br />
<br />
In these cases, the programmer has two choices, either to pass the buck and throw the Exception, or to catch it in a try-catch block.<br />
<br />
Now with this in mind, you need need to make the decision of what type of Exception you want to create, either one the user has to handle, or one which will inevitably stall the program.<br />
To aid with that decision, it is always good to study current Exceptions and take into consideration their uses;<br />
<br />
E.g. If <font color="#800080"><b>NullPointerException</b></font> was a checked Exception, programmers would need to flood their code with try catches for every scenario where there was a possibility of a method returning null,<br />
which in all honesty, would be a massive and ugly pain, so It makes sense for it to be an unchecked Exception instead.<br />
<br />
But for <font color="#800080"><b>IOException</b></font>, it makes more sense to force the programmer handle events such as 'File not found' so that program offers an alternative means of proceeding to the user, or to simple terminate gracefully instead of allowing the programmer to let the program stall.<br />
<br />
<br />
<font color="#008080"><u><b><span style="font-family:  Verdana"><font size="4">Examples in code</font></span></b></u></font><br />
<br />
Consider this system login program which prompts the user for a username, password and validates those inputs against the stored credentials. <br />
The flow of execution will go one of two ways, either it will tell the user how they've successfully logged in, or it will throw an SecurityBreachException which when caught, will tell the user the login process failed. This is very basic example but will do nicely for demonstration purposes.<br />
<br />
<br />
<b><u><font size="3">Example #1 - Checked.</font></u></b><br />
<div class="bbcode_container">
                
<div class="bbcode_description" style="height:24px; background-image: url('http://www.javaprogrammingforums.com/images/javacode2.png'); background-repeat: no-repeat; margin-bottom:0px"></div>

<div class="bbcode_code" style="height:96px;"><div class="java" style="font-family:monospace;"><pre style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #7F0055; font-weight: bold;">public</span> <span style="color: #7F0055; font-weight: bold;">class</span> SecurityBreachException <span style="color: #7F0055; font-weight: bold;">extends</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a><span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #7F0055; font-weight: bold;">public</span> SecurityBreachException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #7F0055; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">&quot;Attempted Security Breach - User attempted to breach security&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</div> <br />
As the <b><font color="#800080">SecurityBreachException</font></b> class extends Exception, it means this will be a <b>checked</b> exception, and will therefore need to be handled by the programmer.<br />
Using its super class constructor (<a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html#Exception(java.lang.String)" target="_blank">Exception(String message)</a>), I set the message for my exception to <font color="#0000FF">&quot;Attempted Security Breach - User attempted to breach security&quot;</font> which is the message that will now be registered with the thrown exception.<br />
<br />
Although I do it here, I wouldn't exactly recommend you always extend the broad classes, such as Exception for checked and RuntimeException for unchecked, but extend relevant exceptions, such as IOException for anything media related, or in this case I probably should have extended <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalAccessException.html" target="_blank">IllegalAccessException</a> - as in any case; specifics are best.<br />
<br />
Now lets have a look at it in action.<br />
<br />
<div class="bbcode_container">
                
<div class="bbcode_description" style="height:24px; background-image: url('http://www.javaprogrammingforums.com/images/javacode2.png'); background-repeat: no-repeat; margin-bottom:0px"></div>

<div class="bbcode_code" style="height:372px;"><div class="java" style="font-family:monospace;"><pre style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #7F0055; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Scanner</span><span style="color: #000000;">;</span>
&nbsp;
<span style="color: #7F0055; font-weight: bold;">public</span> <span style="color: #7F0055; font-weight: bold;">class</span> SecurityLogin <span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #3F7F5F; font-style: italic;">//Only valid credentials &#91;0&#93; = username, &#91;1&#93; = password</span>
	<span style="color: #7F0055; font-weight: bold;">private</span> <span style="color: #7F0055; font-weight: bold;">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> admin <span style="color: #000000;">=</span> <span style="color: #000000;">&#123;</span> <span style="color: #0000ff;">&quot;jpf&quot;</span>, <span style="color: #0000ff;">&quot;admin&quot;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">;</span>
&nbsp;
	<span style="color: #7F0055; font-weight: bold;">private</span> SecurityLogin<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Validates whether user input matches stored credentials
	 * @param user User's attempt at 'Username'
	 * @param pass User's attempt at 'Password'
	 * @throws SecurityBreachException Thrown when details don't match
	 */</span>
	<span style="color: #7F0055; font-weight: bold;">public</span> <span style="color: #7F0055; font-weight: bold;">static</span> <span style="color: #7F0055; font-weight: bold;">void</span> validateLogin<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> user, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> pass<span style="color: #000000;">&#41;</span> <span style="color: #7F0055; font-weight: bold;">throws</span> SecurityBreachException <span style="color: #000000;">&#123;</span>
		<span style="color: #7F0055; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>user.<span style="color: #000000;">equals</span><span style="color: #000000;">&#40;</span>admin<span style="color: #000000;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&amp;&amp;</span> pass.<span style="color: #000000;">equals</span><span style="color: #000000;">&#40;</span>admin<span style="color: #000000;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #000000;">out</span>.<span style="color: #000000;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">&quot;Welcome %s, you are now successfully logged in.&quot;</span>, user<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #3F7F5F; font-style: italic;">//match</span>
		<span style="color: #000000;">&#125;</span> <span style="color: #7F0055; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #7F0055; font-weight: bold;">throw</span> <span style="color: #7F0055; font-weight: bold;">new</span> SecurityBreachException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #3F7F5F; font-style: italic;">//No match, throw exception</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #7F0055; font-weight: bold;">public</span> <span style="color: #7F0055; font-weight: bold;">static</span> <span style="color: #7F0055; font-weight: bold;">void</span> main<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>... <span style="color: #000000;">args</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		Scanner input <span style="color: #000000;">=</span> <span style="color: #7F0055; font-weight: bold;">new</span> Scanner<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #000000;">in</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #000000;">out</span>.<span style="color: #000000;">println</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">&quot;Enter a Username: &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> iUser <span style="color: #000000;">=</span> input.<span style="color: #000000;">next</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #000000;">out</span>.<span style="color: #000000;">println</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">&quot;Enter a Password: &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> iPass <span style="color: #000000;">=</span> input.<span style="color: #000000;">next</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
		<span style="color: #7F0055; font-weight: bold;">try</span> <span style="color: #000000;">&#123;</span>
			SecurityLogin.<span style="color: #000000;">validateLogin</span><span style="color: #000000;">&#40;</span>iUser, iPass<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
		<span style="color: #000000;">&#125;</span> <span style="color: #7F0055; font-weight: bold;">catch</span> <span style="color: #000000;">&#40;</span>SecurityBreachException e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #000000;">out</span>.<span style="color: #000000;">println</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">&quot;Login Failed!&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</div> <br />
What you need to realise here is that, when dealing with checked exceptions, every time you decide to throw an exception, you need to declare that the method from which it is called throws that exception too, be it a 'normal' method or the main method. To do this, you need to use the throw<b>s</b> clause, e.g. &quot;void myMethod() throws BoomerangException&quot;.<br />
<br />
Within the main method, I catch any <b><font color="#800080">SecurityBreachException</font></b>'s within a try-catch block, and print a &quot;Login Failed&quot; message on failure, but I could have just set the main method to also throw the exception, but that would have simply led to the program stalling once it occurred.<br />
<br />
If you were to execute the program with correct inputs, the output on the command line would look like the following:<br />
<div style="margin:20px; margin-top:5px">

	<div class="smallfont" style="height:27px; background-image: url('http://www.javaprogrammingforums.com/images/console.png'); background-repeat: no-repeat;">&nbsp;</div>

	<pre class="alt2" dir="ltr" style="
		margin: 0px;
		padding: 1px;
		border: 1px solid;
                border-color: #99BAF3;
                background-color:white;
                white-space:nowrap;
		width: 754px;
		height: auto;
                min-height:100px;
		text-align: left;
		overflow: auto"><br />
Enter a Username: <br />
jpf<br />
Enter a Password: <br />
admin<br />
Welcome jpf, you are now successfully logged in.<br />
<br></br></pre>
</div><br />
<br />
If it was executed with incorrect inputs however, the outcome would be as follows:<br />
<div style="margin:20px; margin-top:5px">

	<div class="smallfont" style="height:27px; background-image: url('http://www.javaprogrammingforums.com/images/console.png'); background-repeat: no-repeat;">&nbsp;</div>

	<pre class="alt2" dir="ltr" style="
		margin: 0px;
		padding: 1px;
		border: 1px solid;
                border-color: #99BAF3;
                background-color:white;
                white-space:nowrap;
		width: 754px;
		height: auto;
                min-height:100px;
		text-align: left;
		overflow: auto"><br />
Enter a Username: <br />
Hax0RZ<br />
Enter a Password: <br />
admin<br />
Login Failed!<br />
<br></br></pre>
</div><br />
<br />
If you were to replace<br />
System.out.println(<font color="#0000FF">&quot;Login Failed!&quot;</font>) with System.out.println(e.getMessage()) however,<br />
it would print out <font color="#0000FF">&quot;Attempted Security Breach - User attempted to breach security&quot;</font> (The message registered with the exception).<br />
<br />
Now you've seen an example of a checked exception, lets take a look at an unchecked implementation.<br />
<br />
<br />
<br />
<b><u><font size="3">Example #2 - Unchecked</font></u></b><br />
<br />
<div class="bbcode_container">
                
<div class="bbcode_description" style="height:24px; background-image: url('http://www.javaprogrammingforums.com/images/javacode2.png'); background-repeat: no-repeat; margin-bottom:0px"></div>

<div class="bbcode_code" style="height:96px;"><div class="java" style="font-family:monospace;"><pre style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #7F0055; font-weight: bold;">public</span> <span style="color: #7F0055; font-weight: bold;">class</span> SecurityBreachException <span style="color: #7F0055; font-weight: bold;">extends</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aillegalargumentexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IllegalArgumentException</span></a><span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #7F0055; font-weight: bold;">public</span> SecurityBreachException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #7F0055; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">&quot;Attempted Security Breach - User attempted to breach security&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</div> <br />
Notice how I've replaced 'extends <b><font color="#800080">Exception</font></b>' with 'extends <b><font color="#800080">IllegalArgumentException</font></b>';<br />
there are two reasons for this, mainly because it is a subclass of RuntimeException, meaning unchecked<br />
and secondly, because it is more specific that simply extending RuntimeException. Everything else in the class is left untouched, meaning it is a very simple transition.<br />
<br />
Here is the new implementation, in which you should notice a few differences.<br />
<br />
<div class="bbcode_container">
                
<div class="bbcode_description" style="height:24px; background-image: url('http://www.javaprogrammingforums.com/images/javacode2.png'); background-repeat: no-repeat; margin-bottom:0px"></div>

<div class="bbcode_code" style="height:372px;"><div class="java" style="font-family:monospace;"><pre style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #7F0055; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Scanner</span><span style="color: #000000;">;</span>
&nbsp;
<span style="color: #7F0055; font-weight: bold;">public</span> <span style="color: #7F0055; font-weight: bold;">class</span> SecurityLogin <span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #3F7F5F; font-style: italic;">// Only valid credentials &#91;0&#93; = username, &#91;1&#93; = password</span>
	<span style="color: #7F0055; font-weight: bold;">private</span> <span style="color: #7F0055; font-weight: bold;">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> admin <span style="color: #000000;">=</span> <span style="color: #000000;">&#123;</span> <span style="color: #0000ff;">&quot;jpf&quot;</span>, <span style="color: #0000ff;">&quot;admin&quot;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">;</span>
&nbsp;
	<span style="color: #7F0055; font-weight: bold;">private</span> SecurityLogin<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Validates whether user input matches stored credentials
	 * @param user User's attempt at 'Username'
	 * @param pass User's attempt at 'Password'
	 */</span>
	<span style="color: #7F0055; font-weight: bold;">public</span> <span style="color: #7F0055; font-weight: bold;">static</span> <span style="color: #7F0055; font-weight: bold;">void</span> validateLogin<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> user, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> pass<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #7F0055; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>user.<span style="color: #000000;">equals</span><span style="color: #000000;">&#40;</span>admin<span style="color: #000000;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&amp;&amp;</span> pass.<span style="color: #000000;">equals</span><span style="color: #000000;">&#40;</span>admin<span style="color: #000000;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #000000;">out</span>.<span style="color: #000000;">printf</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">&quot;Welcome %s, you are now successfully logged in.&quot;</span>, user<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #3F7F5F; font-style: italic;">// match</span>
		<span style="color: #000000;">&#125;</span> <span style="color: #7F0055; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #7F0055; font-weight: bold;">throw</span> <span style="color: #7F0055; font-weight: bold;">new</span> SecurityBreachException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #3F7F5F; font-style: italic;">// No match, throw exception</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #7F0055; font-weight: bold;">public</span> <span style="color: #7F0055; font-weight: bold;">static</span> <span style="color: #7F0055; font-weight: bold;">void</span> main<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>... <span style="color: #000000;">args</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		Scanner input <span style="color: #000000;">=</span> <span style="color: #7F0055; font-weight: bold;">new</span> Scanner<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #000000;">in</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #000000;">out</span>.<span style="color: #000000;">println</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">&quot;Enter a Username: &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> iUser <span style="color: #000000;">=</span> input.<span style="color: #000000;">next</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #000000;">out</span>.<span style="color: #000000;">println</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">&quot;Enter a Password: &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> iPass <span style="color: #000000;">=</span> input.<span style="color: #000000;">next</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
		SecurityLogin.<span style="color: #000000;">validateLogin</span><span style="color: #000000;">&#40;</span>iUser, iPass<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</div> <br />
I'm sure you noticed quite a few things which looked different this time around, mainly how much tidier the code looks as much as anything else.<br />
This is because there is no mention of clauses such as try-catch or throw<b>s</b>, and unlike the previous example this code is correct and compilable <b>without</b> a try-catch block as the compiler doesn't force you to handle anything.<br />
<br />
However, unchecked doesn't mean uncheckable, as you could always surround the code with a try-catch block and handle any exceptions which might arise, but if that would be a common approach for the problem, it might be better reconsider your approach and possibly have the Exception be checked instead of unchecked.<br />
<br />
With this approach - if the login is successful, the user will receive the same success message as in the previous example, but if the inputs do not match, the program will stall and spit out the following message:<br />
<br />
<div style="margin:20px; margin-top:5px">

	<div class="smallfont" style="height:27px; background-image: url('http://www.javaprogrammingforums.com/images/console.png'); background-repeat: no-repeat;">&nbsp;</div>

	<pre class="alt2" dir="ltr" style="
		margin: 0px;
		padding: 1px;
		border: 1px solid;
                border-color: #99BAF3;
                background-color:white;
                white-space:nowrap;
		width: 754px;
		height: auto;
                min-height:100px;
		text-align: left;
                color: red;
		overflow: auto"><br />
Exception in thread &quot;main&quot; SecurityBreachException: Attempted Security Breach - User attempted to breach security<br />
	at SecurityLogin.validateLogin(SecurityLogin.java:20)<br />
	at SecurityLogin.main(SecurityLogin.java:33)<br />
<br></br></pre>
</div><br />
Which isn't the prettiest of sights :(<br />
<br />
And well... that's it :) - thanks for reading and I hope you learnt something. <br />
<br />
Feel free to ask more questions &amp; the like.</blockquote>

]]></content:encoded>
			<dc:creator>newbie</dc:creator>
			<guid isPermaLink="true">http://www.javaprogrammingforums.com/blogs/newbie/30-create-your-own-exceptions.html</guid>
		</item>
	</channel>
</rss>
