Throw and catch exception in 2 classes

Discussion in 'Java' started by Matt, Jun 10, 2004.

  1. Matt

    Matt Guest

    I am new to exceptions. I have 2 classes A and B, and process() method in class
    A will call generateString() method in class B. Exception can happen in class B,
    and class A needs to take care of that exception. Below is my code so far,
    but not sure what I do in the comment I put below.

    public class A
    {
    public void process()
    {
    try
    { B b = new B();
    String response = b.generateString("a string");
    }
    catch()
    { //catch exception from class B
    }
    }
    }

    public class B
    {
    public String generateString(String s)
    { //can throw exception if generation has problem
    }
    }


    Please advise. Thanks!!
    Matt, Jun 10, 2004
    #1
    1. Advertising

  2. Matt

    Andy Fish Guest

    "Matt" <> wrote in message
    news:...
    > I am new to exceptions. I have 2 classes A and B, and process() method in

    class
    > A will call generateString() method in class B. Exception can happen in

    class B,
    > and class A needs to take care of that exception. Below is my code so far,
    > but not sure what I do in the comment I put below.
    >
    > public class A
    > {
    > public void process()
    > {
    > try
    > { B b = new B();
    > String response = b.generateString("a string");
    > }
    > catch()
    > { //catch exception from class B
    > }
    > }
    > }
    >
    > public class B
    > {
    > public String generateString(String s)
    > { //can throw exception if generation has problem
    > }
    > }
    >
    >


    An exception is an object like any other. to distinguish your exception from
    others you will probably want to create your own subclass of Exception (say
    MyException). then declare the method generateString as

    public String generateString(String s) throws MyException

    when you want to throw the exception inside the method, you simply

    throw new MyException(...);

    and to catch it inside A, you need to do

    try {
    ...
    } catch (MyException ex) {
    ... do stuff with ex
    }

    I would advise you to read up about exceptions in a book or suchlike before
    you start using them in earnest though.

    > Please advise. Thanks!!
    Andy Fish, Jun 10, 2004
    #2
    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. Kerri
    Replies:
    2
    Views:
    13,022
    Kevin Spencer
    Oct 27, 2003
  2. Jon Maz
    Replies:
    7
    Views:
    4,271
    Jon Maz
    Oct 25, 2004
  3. Matt
    Replies:
    1
    Views:
    405
    Michael Rauscher
    Jun 12, 2004
  4. Replies:
    15
    Views:
    7,532
    Roedy Green
    Sep 8, 2005
  5. Daimler
    Replies:
    7
    Views:
    1,033
    Daimler
    Jan 26, 2007
Loading...

Share This Page