how to rethrow exception in Java

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gil

    how to rethrow exception in Java

    In C++, I can rethrow the exception I just caught
    with the throw statement. Can I do something
    similar in Java?

    } catch (Exception ex) {
    throw;
    }
  • Ryan Stewart

    #2
    Re: how to rethrow exception in Java

    "Gil" <brightoceanlig ht@hotmail.com> wrote in message
    news:adc2ca29.0 403170433.77086 376@posting.goo gle.com...[color=blue]
    > In C++, I can rethrow the exception I just caught
    > with the throw statement. Can I do something
    > similar in Java?
    >
    > } catch (Exception ex) {
    > throw;
    > }[/color]
    throw ex;

    Please post to comp.lang.java. help instead of here in the future.


    Comment

    • Stewart Gordon

      #3
      Re: how to rethrow exception in Java

      Gil wrote:
      [color=blue]
      > In C++, I can rethrow the exception I just caught
      > with the throw statement. Can I do something
      > similar in Java?
      >
      > } catch (Exception ex) {[/color]
      throw ex;[color=blue]
      > }[/color]

      works perfectly fine for me.

      Stewart.

      --
      My e-mail is valid but not my primary mailbox, aside from its being the
      unfortunate victim of intensive mail-bombing at the moment. Please keep
      replies on the 'group where everyone may benefit.

      Comment

      • Henrik Eiriksson

        #4
        Re: how to rethrow exception in Java

        Gil wrote:
        [color=blue]
        > In C++, I can rethrow the exception I just caught
        > with the throw statement. Can I do something
        > similar in Java?
        >
        > } catch (Exception ex) {
        > throw;
        > }[/color]

        you can throw a new exception upon catch including an informative message
        and the original exception like this:

        try {
        //...code...
        } catch(Exception ex) {
        throw new Exception("your informative message", ex);
        }

        the new exception thrown can be your own exception type, specific to your
        application. Read more here:
        This Java tutorial describes exceptions, basic input/output, concurrency, regular expressions, and the platform environment



        Comment

        • Yoyoma_2

          #5
          Re: how to rethrow exception in Java

          Stewart Gordon wrote:[color=blue]
          > Gil wrote:
          >[color=green]
          >> In C++, I can rethrow the exception I just caught
          >> with the throw statement. Can I do something similar in Java?
          >>
          >> } catch (Exception ex) {[/color]
          >
          > throw ex;
          >[color=green]
          >> }[/color]
          >
          >
          > works perfectly fine for me.
          >
          > Stewart.
          >[/color]

          If you are throwing exactly the same exception you catch, you don't have
          to do the try catch for that exception.

          ex:

          public URL createURL( Context ctx ) throws MalformedURLExc eption{
          return new URL( ctx.getMyUrlStr ing() );
          }

          Comment

          • Yoyoma_2

            #6
            Re: how to rethrow exception in Java

            Ryan Stewart wrote:
            [color=blue]
            > "Gil" <brightoceanlig ht@hotmail.com> wrote in message
            > news:adc2ca29.0 403170433.77086 376@posting.goo gle.com...
            >[color=green]
            >>In C++, I can rethrow the exception I just caught
            >>with the throw statement. Can I do something
            >>similar in Java?
            >>
            >>} catch (Exception ex) {
            >> throw;
            >>}[/color]
            >
            > throw ex;
            >
            > Please post to comp.lang.java. help instead of here in the future.[/color]

            For clarity, whats "java.help" do vs comp.lang.java? Is there a web page
            for this newsgroup?
            thanks

            Comment

            • Ryan Stewart

              #7
              Re: how to rethrow exception in Java

              "Yoyoma_2" <Yoyoma_2@[at-]Hotmail.com> wrote in message
              news:j356c.9238 9$Up2.82516@pd7 tw1no...[color=blue]
              > Ryan Stewart wrote:
              >[color=green]
              > > "Gil" <brightoceanlig ht@hotmail.com> wrote in message
              > > news:adc2ca29.0 403170433.77086 376@posting.goo gle.com...
              > >[color=darkred]
              > >>In C++, I can rethrow the exception I just caught
              > >>with the throw statement. Can I do something
              > >>similar in Java?
              > >>
              > >>} catch (Exception ex) {
              > >> throw;
              > >>}[/color]
              > >
              > > throw ex;
              > >
              > > Please post to comp.lang.java. help instead of here in the future.[/color]
              >
              > For clarity, whats "java.help" do vs comp.lang.java? Is there a web page
              > for this newsgroup?
              > thanks[/color]
              It figures you'd ask the one time I don't write it out :) I don't know all
              the details, but apparently the comp.lang.java hierarchy (possibly the whole
              comp.* hierarchy?) is actually governed by some body or other, and this
              group, comp.lang.java, is not supposed to exist. Therefore not all news
              servers carry it and not all users can access it. Other groups, such as
              comp.lang.java. help (for beginners) or c.l.j.programme r (more advanced) and
              lots of others are "official" groups and widely carried by news servers.


              Comment

              • Stewart Gordon

                #8
                Re: how to rethrow exception in Java

                Yoyoma_2 wrote:

                <snip>[color=blue]
                > If you are throwing exactly the same exception you catch, you don't have
                > to do the try catch for that exception.
                >
                > ex:
                >
                > public URL createURL( Context ctx ) throws MalformedURLExc eption{
                > return new URL( ctx.getMyUrlStr ing() );
                > }[/color]

                I just assumed that the OP wanted to do something in the catch besides
                rethrowing it, but which had been omitted from the post.

                Stewart.

                --
                My e-mail is valid but not my primary mailbox, aside from its being the
                unfortunate victim of intensive mail-bombing at the moment. Please keep
                replies on the 'group where everyone may benefit.

                Comment

                Working...