<?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>Michael Grace &#187; Java</title>
	<atom:link href="http://geek.michaelgrace.org/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://geek.michaelgrace.org</link>
	<description>All my geek in one place</description>
	<lastBuildDate>Tue, 19 Feb 2013 16:43:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Java Global Exception Handling</title>
		<link>http://geek.michaelgrace.org/2011/09/java-global-exception-handling/</link>
		<comments>http://geek.michaelgrace.org/2011/09/java-global-exception-handling/#comments</comments>
		<pubDate>Wed, 07 Sep 2011 15:56:56 +0000</pubDate>
		<dc:creator>MikeGrace</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[30 days of java]]></category>

		<guid isPermaLink="false">http://geek.michaelgrace.org/?p=1955</guid>
		<description><![CDATA[It&#8217;s been a while since I have worked with Java and it&#8217;s time for me to brush up so I have decided to do 30 days of Java. I will be attempting to learn something new or explain something about Java each day. In many of my projects I use a remote error collecting service [...]]]></description>
				<content:encoded><![CDATA[<p>It&#8217;s been a while since I have worked with Java and it&#8217;s time for me to brush up so I have decided to do 30 days of Java. I will be attempting to learn something new or explain something about Java each day.</p>
<p>In many of my projects I use a remote error collecting service called <a href="http://www.errorstack.com/">ErrorStack</a>. Love it! My apps throw errors over the internet to it and it generates report and has a myriad of ways to alert me. I do my best to write code that doesn&#8217;t have errors and handles errors and exception gracefully but it still happens. This is where the Java 1.5 <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#setDefaultUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)">setDefaultUncaughtExceptionHandler</a> comes in to play.</p>
<p>I wrote some quick code to test out the feature</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.michaelgrace.sandbox</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GlobalErrorHandling <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Handler handler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Handler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Thread</span>.<span style="color: #006633;">setDefaultUncaughtExceptionHandler</span><span style="color: #009900;">&#40;</span>handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;party on error!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Handler <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Thread</span>.<span style="color: #006633;">UncaughtExceptionHandler</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> uncaughtException<span style="color: #009900;">&#40;</span><span style="color: #003399;">Thread</span> t, <span style="color: #003399;">Throwable</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Where did that error come from?!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;This is where I would report the error to ErrorStack&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>t.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I exported the class to a <a href="http://mikegrace.s3.amazonaws.com/geek-blog/global-error-test.jar">jar</a> using Eclipse and ran it from the command line</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666;">mgrace$ </span>java <span style="color: #660033;">-jar</span> global-error-test.jar 
Where did that error come from?<span style="color: #000000; font-weight: bold;">!</span>
This is where I would report the error to ErrorStack
party on error<span style="color: #000000; font-weight: bold;">!</span>
Thread<span style="color: #7a0874; font-weight: bold;">&#91;</span>main,<span style="color: #000000;">5</span>,main<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></td></tr></table></div>

<p>It worked!</p>
<p>I plan on building a class for making it easy to report errors to ErrorStack and will release it to the world when I do.</p>
<p>Resources: <a href="http://www.nomachetejuggling.com/2006/06/13/java-5-global-exception-handling/">http://www.nomachetejuggling.com/2006/06/13/java-5-global-exception-handling/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://geek.michaelgrace.org/2011/09/java-global-exception-handling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
