Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Passing string to exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing string to exception" Watch "Passing string to exception" New topic
Author

Passing string to exception

Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
I have noticed that some exception constructors will take a String message. I assume this means that the developer can then pass his/her own message to the exception. My question is simple - how do you do this? In the catch block?
i.e.
catch (Exception e)
{
e = new Exception("My message")
}
Thanks,
Mike


Mike<br />SCJP 1.4<br />----------------------------<br />mdcleary@earthlink.net<br />----------------------------<br />There are 10 types of people<br />in the world. Those that <br />understand binary, and those<br />that don't.
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
The code to create an exception is correct. While you might want to construct an exception within a catch block, normally the main reason is to construct an exception of a different type, and rethrow it instead. For example:

If you're using 1.4 or later, a better solution is to embed the entire source exception in the new exception as a cause:

In any case, generally the place where you initially throw an exception is where you construct it with a message.
Jeff


Books: Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
So if I have a constructor which throws an exception, I would construct it there w/ a message? What would that look like?
Thanks,
Mike
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
So if I have a constructor which throws an exception,
I'm not exactly sure that you mean what you've said here.
Perhaps the following example, extending your example above, is what you're asking about.

[ January 25, 2004: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
Originally posted by Michael Cleary:
So if I have a constructor which throws an exception, I would construct it there w/ a message? What would that look like?


Throwing an exception from a constructor is fine to do in Java (but you don't want to do it in C++.
-Jeff-
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Jeff Langr:

Throwing an exception from a constructor is fine to do in Java (but you don't want to do it in C++.

Sure you do. I'm pretty sure that was a major reason exceptions were introduced in C++. If you're constructing an object and find that you can't construct a valid one, throwing an exception is by far the best alternative -- the only other one is to have a "broken" flag in the object, and to check it from every method -- an ugly idiom, common before exceptions came along.
You don't want to throw an exception in a C++ destructor. That's just bad news. But throwing them from constructors is both common and a fine thing to do.


[Jess in Action][AskingGoodQuestions]
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
You are correct, I got it wrong. I do recall that you want to ensure you clean up when throwing an exception from a constructor, which can be a pretty tricky task if you construct multiple things and an exception is thrown inbetween.
-Jeff-
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Passing string to exception
 
Similar Threads
Exception Handling
Class Exception
Variable initialization
Exception Handling
Problem facing with send email program