0

My question is If Suppose CalTransactionFactory is not initialized or class does not exist. Then I want to catch that exception and throw an exception. So it should be coming under RuntimeException or Exception. below is the code

try {
  calTransaction = CalTransactionFactory.create("PDS_GeoLocationService");
  calTransaction.setName("GetLocationByIp");
  calEvent=CalEventFactory.create("PersonalizationGeoLocationService");
  calEvent.setName("IPAddress");
  calEvent.addData("RemoteIP",ipAddress);

  calEvent.completed();             

  calTransaction.setStatus("0");

} catch (RuntimeException re) {
  calTransaction.setStatus(re);
  getLogger().log(LogLevel.ERROR, "CAL is not initialized" +re.getMessage());
  throw re;
}
catch (Exception e) {
  getLogger().log(LogLevel.ERROR, "CAL is not initialized" +e.getMessage());
}
1
  • 2
    What is the question exactly? Commented Mar 9, 2012 at 20:18

3 Answers 3

1

java.lang.ClassNotFoundException is what is thrown when the JVM cannot find the class files through the configured classLoaders. This exception extends Exception.

java.lang.NullPointerException is what is thrown when your variable is null and something tries to access it. It extends RuntimeException, which extends Exception.

Sign up to request clarification or add additional context in comments.

3 Comments

Nit: ClassNotFoundError is thrown when the JVM cannot find the class files during class linking, and ClassNotFoundException is what is thrown when the equivalent happens via the reflective APIs.
@Athrawn17 so in my case I should remove runtimeexception and use exception catch block only?
It depends on what your goal is. If you want to log the specific cause, you would catch the specific error you are looking for. If you want to catch "the world" then catch(Exception e) will do that.
0

catch(Exception e) will catch ANY exception that is a subclass of Exception (virtually all of them)

3 Comments

@deporter - If I take the liberty of interpreting what I think the OP is asking, I believe he is concerned with how to structure his catch in order to properly handle the exceptions he is getting. Thus, an answer.
@deporter: Length or triviality is not a criterion for whether an answer should be an answer or a comment. The sole criterion should be whether it answers the question.
"ANY exception" -> "ANY throwable"?
0

RuntimeException is a type of Exception. Where it is caught depends on the type of exception thrown by the create method. If the type of exception is or extends RuntimeException it will be caught there, otherwise it will be caught in Exception. Is that the question?

It is typically bad practice to log in a catch block that is going to rethrow the exception.

Also, if it is a RuntimeException, you don't know if calTransaction is initialized so setting the status might result in a NullPointerException. If that is what you're trying to avoid, you could have it throw some type of InitializationException, or move it to a previous try/catch.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.