Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the term for catching and rethrowing a java exception?

I seem to remember a very specific term for situations like this:

try {
  operation();
} catch (SomeException se) {
  throw new OtherException(se);
}

-or-

try {
  operation();
} catch (SomeException se) {
  throw new OtherException("new message, forget original exception");
}

and I'd like to use that correct term in some documentation. Can someone please jog my memory?


EDIT: As I understand the link quoted by JB Nizet, chaining is the act of passing the original exception to the constructor of the new one so the stack trace will include the original one. But that's not the concept I'm looking for. Sorry if my question ended up being misleading this way.

like image 870
Carl Smotricz Avatar asked Dec 10 '22 06:12

Carl Smotricz


1 Answers

In response to your edit: In that case, you're just "throwing" an exception. It just so happens to be thrown in a catch block. Instead of using potentially confusing terminology such as "transforming" (which I think implies mutation, rather than something new), I suggest you just say what you are doing explicitly.


You are "wrapping" the caught exception with another. Another term for it is "exception chaining"

Note, that rethrowing an exception is different because you are not creating a new exception, just catching it to do some logging (or something) and then throwing it again. To demonstrate:

try {
  operation();
}
catch (SomeException se) {
  throw se;
}
like image 159
Jeremy Avatar answered Dec 22 '22 00:12

Jeremy