Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing string to exception

 
Michael Cleary
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Cleary
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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-
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic