Mockito provides an API to raise errors during testing. Throwable is super class of all exceptions ( & errors). But, for a short answer: Throwable is the root of all sequence-interrupting events, including java.lang.Exception. There are times when we want to stub the exceptions. We use throw keyword to explicitly throw an exception from the code. throw & throws are keyword in java. We wrapped 1st exception (NPE) into IllegalArgumentException and specified an exception message. I have yet to see an explanation of when one should use each. Mistake 1: Specify a java.lang.Exception or java.lang.Throwable. There are two types of exceptions in Java: checked (compile time) exceptions and unchecked (runtime) exceptions. Exceptions : An Exception “indicates conditions that a reasonable application might want to catch.” Exceptions are the conditions that occur at runtime and may cause the termination of program. throw Throwable-instance; return_type method_name(parameter-list) throws ExceptionClass_list {// body of method} Followed by: The throw keyword is followed by exception object. When I read texts on writing your own ‘exceptions’, I see examples of Throwable being used in the catch block and other texts show new Exception() being used in the catch block. It may be any method or static block. You can use any subclass of java.lang.Throwable in a throws clause. Throwable sound like an interface, it’s true.. It provides methods thenThrow(Throwable) and doThrow(Throwable), so one can stub the mock to throw an exception when the stubbed method is invoked. Summary – throw, throws & Throwable in java. throws signifies, the kind of exception, the method can throw throw is used to throw exception, from method or executable block. Given: Throwable is Exception’s superclass. throw is a statement that causes an exception to be thrown: The Exception class provides specific constructor methods that accept a Throwable as a parameter. The throws keyword is followed by the list of the exception classes that can occur in the method. Dynamic programming vs memoization vs tabulation; Big O notation explained; Sliding Window Algorithm with Example; What makes a good loop invariant? The detailMessage parameter gives the details of the message for this exception, and the throwable parameter gives the cause of this exception. Mockito is an openRead More This exception must be a subclass of Throwable. Types of exceptions. In this article, we will look into stubbing with exceptions. Exceptions are divided into two catagories : checked and unchecked exceptions. But they are recoverable using try, catch and throw keywords. Good exceptions handling is a key to keep our application working after an appearance of those unpleasant moments. James Gosling said: The reason that the Throwable and the rest of those guys are not interfaces is because we decided, or I decided fairly early on. Generating a random point within a circle (uniformly) Java: throw vs throws vs Throwable. But in fact, it’s a class. Otherwise, you lose the stack trace and message of the original exception which will make it difficult to analyze the exceptional event that caused your exception. Is Throwable Interface Or Class. As I explained in one of my previous posts, you either need to specify or handle a checked exception.But checked exceptions are not the only ones you can specify. Usually, application developers will want to subclass java.lang.Exception. Also, it can be a Throwable itself. Little bit in Exception theoretically, but i dont know how to handle Throwable, because my sonar violation part showing like avoid catch throwable, so if i used Exception instead of Throwable, what will i get while running application. For more details, I encourage you to ask the folks in the Java In General forum.