Re: Avoid creating a stacktrace prior to JDK 1.7
Fri, 30 Sep 2011 15:57:57 +0200, /Jan Burse/:
I have the following code deep down in a
recursion which eats up a lot of stack.
The recursion is not shown but only
the code:
Class<?> class;
try {
class = Class.forName(str);
} catch (ClassNotFoundException x) {
class = null;
}
Will it every time I call it for a
non-existing class build the whole stack
trace for the exception x and then
immediately forget about it?
I guess so.
[...]
Can I instruct an exception
to not fill the stack trace? And way
arround?
You may perform a check which avoids the exceptional situation like:
Class<?> class;
try {
class = (Class.getResource(str + ".class") != null)
? Class.forName(str)
: null;
} catch (ClassNotFoundException x) {
class = null;
}
The Class.forName(String) could still fail if the access to the
resource content is restricted, I imagine.
--
Stanimir