Results 1 to 5 of 5
Thread: throw exception
- 07-10-2007, 04:57 AM #1Member
- Join Date
- Jul 2007
- Rep Power
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.
- 07-10-2007, 12:43 PM #2why do some methods throw exceptions and how the calling method handles those exceptions?
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
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:
void sendMessage(String username, String message)
- 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.
- 07-10-2007, 07:15 PM #3Member
- Join Date
- Jul 2007
- Rep Power
Thanks for solving my query.
- 04-23-2009, 03:57 AM #4
Help I need to make money fast
Can any one show me how to work at home and make money fast using a website
If I do not make money fast I will find myself homeless
- 04-23-2009, 04:35 AM #5
nellunses... this is a Java forum for sw developers. Although websites is related to Java programming, developing websites is not the purpose of this forum. I suggest studying books and using Google to find sites that explain how to develop websites.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- By Deon in forum New To JavaReplies: 7Last Post: 02-11-2010, 06:46 PM
- By Oktam in forum New To JavaReplies: 2Last Post: 03-23-2008, 08:01 PM
- By desmond5 in forum New To JavaReplies: 1Last Post: 03-08-2008, 07:41 PM
- By Poonam in forum New To JavaReplies: 7Last Post: 02-06-2008, 05:52 PM
- By Camden in forum New To JavaReplies: 2Last Post: 11-27-2007, 12:50 AM