when to throws or catch exception in chains of method calls?

Discussion in 'Java' started by Matt, Jul 1, 2004.

  1. Matt

    Matt Guest

    I want to know what is the best practice to throw or catch exception
    in
    chains of method calls. Consider the following cases with method
    call sequence: f->g->h->i. All the following approaches will work, but
    want to know which is the best approach. Please advise. thanks!!

    =====================================================
    Approach #1: declare try/catch block only in FIRST CALLING METHOD,
    which is
    method f. Method f will catch all exceptions that method f, g, h will
    thrown.
    Method f don't throw any exception since method f is FIRST CALLING
    METHOD.
    And the rest will just throw the exception.
    =====================================================

    void f()
    { try
    { //code
    g();
    }
    catch(exception0)
    { //handle its own method f's exception0
    }
    catch(exception1)
    { //handle method g's exception1
    }
    catch(exception2)
    { //handle method h's exception2
    }
    }

    //method g throws exception1, and exception2 is for method h
    void g() throws exception1, exception2
    { //code
    h();
    }

    //method h throws exception2
    void h() throws exception2
    { //code
    i();
    }

    //method i no exception thrown
    void i()
    { //code
    }

    ===========================================
    Approach #2: just catch its own exception on each method, no need to
    take
    care other methods' exception because exception already be catched.
    ============================================
    //method f catch its own exception0, no need to take care of other
    methods' exceptions
    void f()
    { try
    { //code
    g();
    }
    catch(exception0)
    { //handle its own method f's exception0
    }
    }

    //method g catch its own exception1, no need to take care of method
    h's exception2
    void g()
    { try
    { //code
    h();
    }
    catch(exception1)
    { //handle exception1
    }
    }

    //method h catch its own exception2
    void h()
    { try
    { //code
    i();
    }
    catch(exception2)
    { //handle exception2
    }
    }

    //method i has no exception
    void i()
    { //code
    }

    =================================================
    Approach #3: throws exception and catch exception for each method that
    can throw exception
    ==================================================

    void f() throws exception1, exception2
    { try
    { //code
    g();
    }
    catch(exception0)
    { //handle its own method f's exception0
    }
    catch(exception1)
    { //handle method g's exception1
    }
    catch(exception2)
    { //handle method h's exception2
    }
    }

    //method g throws exception1, and exception2 is for method h
    void g() throws exception1, exception2
    { try
    { //code
    h();
    }
    catch(exception1)
    { //handle exception1
    }
    catch(exception2)
    { //handle exception2
    }
    }

    //method h throws exception2
    void h() throws exception2
    { try
    { //code
    i();
    }
    catch(exception2)
    { //handle exception2
    }
    }

    //method i no exception thrown
    void i()
    { //code
    }
     
    Matt, Jul 1, 2004
    #1
    1. Advertising

  2. Matt

    Tony Morris Guest

    I strongly suggest you read before posting to this newsgroup.
    Also, I believe that there are other newsgroups more suited for trivial
    questions (I don't really care - others might).

    http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html
    http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html
    http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html

    Did mention?
    http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform


    "Matt" <> wrote in message
    news:...
    > I want to know what is the best practice to throw or catch exception
    > in
    > chains of method calls. Consider the following cases with method
    > call sequence: f->g->h->i. All the following approaches will work, but
    > want to know which is the best approach. Please advise. thanks!!
    >
    > =====================================================
    > Approach #1: declare try/catch block only in FIRST CALLING METHOD,
    > which is
    > method f. Method f will catch all exceptions that method f, g, h will
    > thrown.
    > Method f don't throw any exception since method f is FIRST CALLING
    > METHOD.
    > And the rest will just throw the exception.
    > =====================================================
    >
    > void f()
    > { try
    > { //code
    > g();
    > }
    > catch(exception0)
    > { //handle its own method f's exception0
    > }
    > catch(exception1)
    > { //handle method g's exception1
    > }
    > catch(exception2)
    > { //handle method h's exception2
    > }
    > }
    >
    > //method g throws exception1, and exception2 is for method h
    > void g() throws exception1, exception2
    > { //code
    > h();
    > }
    >
    > //method h throws exception2
    > void h() throws exception2
    > { //code
    > i();
    > }
    >
    > //method i no exception thrown
    > void i()
    > { //code
    > }
    >
    > ===========================================
    > Approach #2: just catch its own exception on each method, no need to
    > take
    > care other methods' exception because exception already be catched.
    > ============================================
    > //method f catch its own exception0, no need to take care of other
    > methods' exceptions
    > void f()
    > { try
    > { //code
    > g();
    > }
    > catch(exception0)
    > { //handle its own method f's exception0
    > }
    > }
    >
    > //method g catch its own exception1, no need to take care of method
    > h's exception2
    > void g()
    > { try
    > { //code
    > h();
    > }
    > catch(exception1)
    > { //handle exception1
    > }
    > }
    >
    > //method h catch its own exception2
    > void h()
    > { try
    > { //code
    > i();
    > }
    > catch(exception2)
    > { //handle exception2
    > }
    > }
    >
    > //method i has no exception
    > void i()
    > { //code
    > }
    >
    > =================================================
    > Approach #3: throws exception and catch exception for each method that
    > can throw exception
    > ==================================================
    >
    > void f() throws exception1, exception2
    > { try
    > { //code
    > g();
    > }
    > catch(exception0)
    > { //handle its own method f's exception0
    > }
    > catch(exception1)
    > { //handle method g's exception1
    > }
    > catch(exception2)
    > { //handle method h's exception2
    > }
    > }
    >
    > //method g throws exception1, and exception2 is for method h
    > void g() throws exception1, exception2
    > { try
    > { //code
    > h();
    > }
    > catch(exception1)
    > { //handle exception1
    > }
    > catch(exception2)
    > { //handle exception2
    > }
    > }
    >
    > //method h throws exception2
    > void h() throws exception2
    > { try
    > { //code
    > i();
    > }
    > catch(exception2)
    > { //handle exception2
    > }
    > }
    >
    > //method i no exception thrown
    > void i()
    > { //code
    > }
     
    Tony Morris, Jul 1, 2004
    #2
    1. Advertising

  3. Matt

    Chris Smith Guest

    Matt wrote:
    > I want to know what is the best practice to throw or catch exception
    > in
    > chains of method calls. Consider the following cases with method
    > call sequence: f->g->h->i. All the following approaches will work, but
    > want to know which is the best approach. Please advise. thanks!!


    What are you doing when you "handle" an exception? Do you need to
    resume processing afterward, or retry something, or just abort it all?
    You should catch the exception in the place that's appropriate to do so.
    For example, if you want to abort all of f() whenever an exception
    occurs, then you probably want to catch all of your exceptions in f...
    otherwise, you'd need to work out some other way of unwinding the stack
    for the aborted operation. On the other hand, if you can fix something
    and retry h, then you probably want to catch h's exception in g, where
    you can immediately retry the operation and continue processing.

    There's no possible way that anyone can give a correct answer to your
    question about which is "best", without understanding your business
    logic. That's kinda like asking whether int or float is the best data
    type for a variable, without bothering to say what the variable will be
    used for.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Jul 1, 2004
    #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. Chris Miller
    Replies:
    4
    Views:
    13,404
    Chris Smith
    Nov 22, 2003
  2. John Black
    Replies:
    8
    Views:
    4,168
    Xenos
    Aug 20, 2004
  3. Replies:
    2
    Views:
    337
  4. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    574
    Daniel Pitts
    Aug 5, 2009
  5. Replies:
    4
    Views:
    105
Loading...

Share This Page