Results 1 to 5 of 5

Thread: throw exception

  1. #1
    GIRISH PATEL is offline Member
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Default 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.

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    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:

    Java 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.

  3. #3
    GIRISH PATEL is offline Member
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Default

    Thanks for solving my query.

  4. #4
    nellunses's Avatar
    nellunses is offline Member
    Join Date
    Apr 2009
    Location
    Canada
    Posts
    1
    Rep Power
    0

    Default Help I need to make money fast

    Hello all

    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

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    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.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Need help on Exception
    By Deon in forum New To Java
    Replies: 7
    Last Post: 02-11-2010, 06:46 PM
  2. exception
    By Oktam in forum New To Java
    Replies: 2
    Last Post: 03-23-2008, 08:01 PM
  3. Replies: 1
    Last Post: 03-08-2008, 07:41 PM
  4. Difference between Throws and Throw
    By Poonam in forum New To Java
    Replies: 7
    Last Post: 02-06-2008, 05:52 PM
  5. Exception
    By Camden in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 12:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •