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 07-10-2007, 05:57 AM
Member
 
Join Date: Jul 2007
Posts: 2
GIRISH PATEL is on a distinguished road
throw exception
why do some methods throw exceptions and how the calling method handles those exceptions?
If the checked exceptions are thrown then what would happen?
Also if unchecked exceptions are thrown then what would happen?

How do i know my method can throw exception and i should include throws statement.

Also please give me the real world example of throwing exception.

Please explain me in detail the concept of throwing exceptions using example.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-10-2007, 01:43 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Quote:
why do some methods throw exceptions and how the calling method handles those exceptions?
Consider creating a network connection. Several things can happen without your control. You can lost internet connection, a transmission error can happen or the other side might close the port you are communicating.

To handle these types of errors appropriately and in an application specific way, exceptions exist in Java.

For example, for your network application, if some exception occurs, based on that exception, you can decide on application specific actions for that error. You might try to connect to another server (a backup server) as an example or you might show the user that his internet connection is lost and ask him to click retry button when he solved his problem with the internet connection.

So consider exceptions as the flexibility you gave to higher level applications for your API. With exceptions, you are giving application developers who use your API a choice! They can show a message to the user or just exit the application..

> How do i know my method can throw exception and i should include throws
> statement.

If you believe that the higher level applciation developer can handle that exception in different ways and should handle that exception, you will throw it. But if you think that there is no value to have such an exception, then just handle it in your own method.

> Also please give me the real world example of throwing exception.

Assume that you are developing a messenger library. You have a method to send a message to a user which has a method signature like:

Code:
void sendMessage(String username, String message)
When you call this method with sendMessage("John", "Hi"), it tries to send a message to the user John. But think about it a little bit. What kind of errors can happen while just trying to do such a simple call:

- User John might not be online: UserNotFoundException
- A network error can happen while transmitting the message: NetworkErrorException
- John might block your username: MessageBlockedException

For different methods there can be different errors. But the idea is to handle all these errors inside a simple try-catch block.

I hope it is clear now.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-10-2007, 08:15 PM
Member
 
Join Date: Jul 2007
Posts: 2
GIRISH PATEL is on a distinguished road
Thanks for solving my query.
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
exception Oktam New To Java 2 03-23-2008 08:01 PM
Trouble with factory method - unhandled exception type Exception desmond5 New To Java 1 03-08-2008 07:41 PM
Difference between Throws and Throw Poonam New To Java 7 02-06-2008 05:52 PM
Need help on Exception Deon New To Java 5 01-26-2008 09:27 AM
Exception Camden New To Java 2 11-27-2007 12:50 AM


All times are GMT +3. The time now is 08:52 AM.


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