By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,806 Members | 1,506 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,806 IT Pros & Developers. It's quick & easy.

how to rethrow exception in Java

P: n/a
Gil
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;
}
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Gil" <br**************@hotmail.com> wrote in message
news:ad**************************@posting.google.c om...
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;
}

throw ex;

Please post to comp.lang.java.help instead of here in the future.
Jul 17 '05 #2

P: n/a
Gil wrote:
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 ex; }


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.
Jul 17 '05 #3

P: n/a
Gil wrote:
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;
}


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:
http://java.sun.com/docs/books/tutor.../throwing.html
Jul 17 '05 #4

P: n/a
Stewart Gordon wrote:
Gil wrote:
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 ex;
}

works perfectly fine for me.

Stewart.


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 MalformedURLException{
return new URL( ctx.getMyUrlString() );
}
Jul 17 '05 #5

P: n/a
Ryan Stewart wrote:
"Gil" <br**************@hotmail.com> wrote in message
news:ad**************************@posting.google.c om...
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;
}


throw ex;

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


For clarity, whats "java.help" do vs comp.lang.java? Is there a web page
for this newsgroup?
thanks
Jul 17 '05 #6

P: n/a
"Yoyoma_2" <Yoyoma_2@[at-]Hotmail.com> wrote in message
news:j356c.92389$Up2.82516@pd7tw1no...
Ryan Stewart wrote:
"Gil" <br**************@hotmail.com> wrote in message
news:ad**************************@posting.google.c om...
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;
}


throw ex;

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


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

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.programmer (more advanced) and
lots of others are "official" groups and widely carried by news servers.
Jul 17 '05 #7

P: n/a
Yoyoma_2 wrote:

<snip>
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 MalformedURLException{
return new URL( ctx.getMyUrlString() );
}


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.
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.