I have read few articles on google but I am still confused when to use what. I have come across checked and unchecked exceptions. I have one exception which I need to define. Let say, when xyz is null, I need to throw the inconsistentcyException which I will catch in the same file and that catch() block will throw one exception which extends runtime exception. This exception which I am defining newly would be used in this file only.

try{
   if(xyz == null) {
       throw new InconsistentException();
   }
   catch(InconsistentException e)
       throw new AlreadyExisitingExceptionExtendingRuntimeException();
   }
}

I need to define the class having Inconsistent Exception. This snippet is written in execute() function and call() function is calling it which is catching the AlreadyExisitingExceptionExtendingRuntimeException(). I don't know which exception to use(runtime or exception). Please explain. I read the SO links but confused. Can you please explain with an example? :) Thanks in advance.

It's your choice. Checked means that any method that calls your method had to deal with the exception somehow. Unchecked means the calling method doesn't need to do anything, but if you do throw the exception then expect the application to crash. Which you chose depends on how likely the exception is, and on whether the app can sensibly deal with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.