Carl Smotricz
Carl Smotricz

Reputation: 67800

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.

Upvotes: 4

Views: 197

Answers (7)

Jeremy
Jeremy

Reputation: 22435

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;
}

Upvotes: 4

Varun Achar
Varun Achar

Reputation: 15109

It's also called as

Exception Chaining

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 692071

What you're doing is commonly called "exception chaining". I was introduced in Java 1.4.

Upvotes: 1

Mike Kwan
Mike Kwan

Reputation: 24467

Exception wrapping or exception chaining

Upvotes: 2

stacker
stacker

Reputation: 68992

It is called re-throwing or exception-translating to harmonize different api's to an abstracter one (often done in the spring framework).

Upvotes: 3

Sam DeHaan
Sam DeHaan

Reputation: 10325

I believe I've seen that referred to as wrapping an Exception. Sorry if that's not the term you're looking for.

Upvotes: 1

SJuan76
SJuan76

Reputation: 24895

I use "rethrow" without any shame.

Anyway what you are doing is encapsulating the exception inside a new one.

Upvotes: 2

Related Questions