newbie: exceptions

Discussion in 'Java' started by R.A.M., Jan 2, 2007.

  1. R.A.M.

    R.A.M. Guest

    Hello,
    I have started learning Java. In a tutorial I found such example
    (pseudocode):

    method1 {
    try {
    call method2;
    } catch (exception e) {
    doErrorProcessing;
    }
    }

    method2 throws exception {
    call method3;
    }

    method3 throws exception {
    call readFile;
    }

    Could you tell me please why method2 needs "throws exception"? Do the
    programmer need to check what exceptions are generated by all called
    methods?
    /RAM/
     
    R.A.M., Jan 2, 2007
    #1
    1. Advertising

  2. R.A.M.

    ck Guest

    R.A.M. wrote:
    > Hello,
    > I have started learning Java. In a tutorial I found such example
    > (pseudocode):
    >
    > method1 {
    > try {
    > call method2;
    > } catch (exception e) {
    > doErrorProcessing;
    > }
    > }
    >
    > method2 throws exception {
    > call method3;
    > }
    >
    > method3 throws exception {
    > call readFile;
    > }
    >
    > Could you tell me please why method2 needs "throws exception"? Do the
    > programmer need to check what exceptions are generated by all called
    > methods?
    > /RAM/


    method3 would attempt to open a file from (disk or where ever) which
    could result in failure due to various reasons (file not found, disk
    read failure etc) Java forces exception to be declared or caught, if
    exception is not caught it needs to be caught in the method invoking
    it. In your example method 3 throws the exception hence method2 needs
    to catch it. Though method2 does not catch it but throws it again.
    Since it is (method2) is invoked in method1 there are two options with
    method1, throw it or catch it(method1 catches it).

    > Could you tell me please why method2 needs "throws exception"?

    method2 could have caught the exception but thats done in method1

    > programmer need to check what exceptions are generated by all called
    > methods?


    Yes

    Hope this helps

    Cheers,
    Ck
    http://www.gfour.net
     
    ck, Jan 2, 2007
    #2
    1. Advertising

  3. R.A.M.

    Eric Sosman Guest

    R.A.M. wrote On 01/02/07 09:32,:
    > Hello,
    > I have started learning Java. In a tutorial I found such example
    > (pseudocode):
    >
    > method1 {
    > try {
    > call method2;
    > } catch (exception e) {
    > doErrorProcessing;
    > }
    > }
    >
    > method2 throws exception {
    > call method3;
    > }
    >
    > method3 throws exception {
    > call readFile;
    > }
    >
    > Could you tell me please why method2 needs "throws exception"? Do the
    > programmer need to check what exceptions are generated by all called
    > methods?


    Yes, or pretty nearly so. There are "checked exceptions"
    and "unchecked exceptions," and you are allowed to ignore the
    latter. For checked exceptions, though, the rules are:

    - If a method can cause a checked exception to be thrown,
    the method must say "throws WhateverException" to warn
    its callers of the possibility.

    - If you call a method that can throw a checked exception,
    you must arrange to do something about the exception if
    one is in fact thrown. The compiler will complain if you
    call such a method and don't make arrangements.

    - One thing you can do with an exception is catch it, the
    way method1 does. If you catch it, the compiler trusts
    that you have done something appropriate to respond to
    the exceptional circumstance.

    - The other thing you can do is pass the exception along
    to your caller; this is what method3 does with exceptions
    thrown by readFile. If you do this, you must announce
    the fact with a "throws WhateverException" of your own
    to warn your callers that an exception might be thrown.

    method3 calls readFile, and since readFile can (presumably)
    throw an exception, method3 must do something about it. In the
    sample, method3 decides to pass the exception along to its own
    caller, which is why method3 announces "throws exception."

    method2 calls method3, which can cause an exception to be
    thrown. True, method3 won't throw the exception itself: it
    will just pass along whatever readFile throws. But method2
    doesn't really care: All it knows is that calling method3 can
    produce an exception, so method2 must do something about it.
    In the sample, method2 decides to pass the exception along
    rather than catching it, so method2 "throws exception" to warn
    its callers.

    method1 calls method2, which can cause an exception to be
    thrown. This time, though, method1 decides to handle the
    problem locally: It catches the thrown exception and executes
    doErrorProcessing code. Since the exception is caught inside
    method1, it never gets out to method1's callers -- and that is
    why method1 does *not* need to announce "throws exception."

    --
     
    Eric Sosman, Jan 2, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Leonard
    Replies:
    7
    Views:
    353
  2. Ahmed Moustafa
    Replies:
    5
    Views:
    30,124
    Chris Smith
    Jul 14, 2004
  3. Paul Miller
    Replies:
    3
    Views:
    1,064
    Alex Martelli
    Nov 12, 2003
  4. Replies:
    3
    Views:
    634
    Sherm Pendley
    Apr 16, 2007
  5. Lie
    Replies:
    3
    Views:
    709
Loading...

Share This Page