Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-09-2008, 11:27 PM
Member
 
Join Date: Feb 2008
Posts: 7
stevemcc is on a distinguished road
[SOLVED] Handle own exception
I have an assignment to make my own exception. The exception need to be thrown when the user enters a string longer than 31 characters. Then next, task and the one I am struggling with is to do the same, but this time correctly handle the exception gracefully.

StringTooLongException.java
Code:
public class StringTooLongException extends Exception { public StringTooLongException(String message) { super(message); } }
TooLongCrashes.java
Code:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class TooLongCrashes { public static void main(String[] args) throws StringTooLongException, IOException { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String message; boolean c = true; while(c) { stdin = new BufferedReader(new InputStreamReader(System.in)); message = stdin.readLine(); if(message.length() >= 32) throw (new StringTooLongException(message)); else System.out.println(message); } } }
TooLogHandled.java
Code:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class TooLongHandled { static TooLongCrashes too; public static void main(String[] args) throws StringTooLongException, IOException { try { too = new TooLongCrashes(); } catch(Exception e) { System.out.println(too.toString()); } } }
In TooLongHandled.java I cannot seem to figure out how I should go about handling the exception.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-10-2008, 05:29 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Seems to me this is work. Where you stuck with.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-10-2008, 05:48 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 839
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Hi steve. All you need to do is:
Code:
public class StringTooLongException extends Exception { public StringTooLongException(String message) { throw new IllegalArgumentException("Exception: String too long."); } }
With this, the output is below on an input of "Hello":
Quote:
Hello
Hello
But on input longer than 32 you see this:
Quote:
123456789012345678901234567890123
Exception in thread "main" java.lang.IllegalArgumentException: Exception: String too long.
at StringTooLongException.<init>(StringTooLongExcepti on.java:4)
at TooLongCrashes.main(TooLongCrashes.java:17)
Java Result: 1
Should this satisfy your question, please mark this thread as 'Solved', if not, definitely let us assist with more on this topic.

My bad.. I didn't see that it actually worked before I made an adjustment.. so, basically you picked up another way to throw an exception.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)

Last edited by CaptainMorgan : 04-10-2008 at 05:51 AM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-10-2008, 05:55 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Steve may be worried, on his code with the exception just the user input string is displayed. May be looking to make much descriptive exception message as Captain says.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble with factory method - unhandled exception type Exception desmond5 New To Java 1 03-08-2008 07:41 PM
Better way to handle exceptions javaplus Advanced Java 2 01-16-2008 07:47 PM
How to handle socket Exception mayank0512 Networking 13 01-10-2008 01:53 PM
handle wrong input int/null Camden New To Java 1 12-16-2007 10:37 PM
how to handle exceptions paty Advanced Java 2 08-05-2007 05:17 AM


All times are GMT +3. The time now is 09:04 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org