Using mockito to test methods which throw uncaught custom exceptions
Declare the exception in the test method. public void testAddUser() throws ServiceException { … }
Declare the exception in the test method. public void testAddUser() throws ServiceException { … }
If you have multiple exception types, and assuming there’s a hierarchy of exceptions (and all derived publicly from some subclass of std::exception,) start from the most specific and continue to more general: try { // throws something } catch ( const MostSpecificException& e ) { // handle custom exception } catch ( const LessSpecificException& e … Read more
Based on my experience with libraries, you should wrap everything (that you can anticipate) in a FooException for a few reasons: People know it came from your classes, or at least, their usage of them. If they see FileNotFoundException they may be looking all over for it. You’re helping them narrow it down. (I realize … Read more
Here’s a quick example of a custom Exception class with special codes: class ErrorWithCode(Exception): def __init__(self, code): self.code = code def __str__(self): return repr(self.code) try: raise ErrorWithCode(1000) except ErrorWithCode as e: print(“Received error with code:”, e.code) Since you were asking about how to use args here’s an additional example… class ErrorWithArgs(Exception): def __init__(self, *args): # … Read more
The standard for creating custom exceptions is to derive from Exception. You can then introduce your own properties/methods and overloaded constructors (if applicable). Here is a basic example of a custom ConnectionFailedException which takes in an extra parameter which is specific to the type of exception. [Serializable] public class ConnectionFailedException : Exception { public ConnectionFailedException(string … Read more
Yes. You just have to use the RAISE_APPLICATION_ERROR function. If you also want to name your exception, you’ll need to use the EXCEPTION_INIT pragma in order to associate the error number to the named exception. Something like SQL> ed Wrote file afiedt.buf 1 declare 2 ex_custom EXCEPTION; 3 PRAGMA EXCEPTION_INIT( ex_custom, -20001 ); 4 begin … Read more
raise already sets the message so you don’t have to pass it to the constructor: class MyCustomError < StandardError attr_reader :object def initialize(object) @object = object end end begin raise MyCustomError.new(“an object”), “a message” rescue MyCustomError => e puts e.message # => “a message” puts e.object # => “an object” end I’ve replaced rescue Exception … Read more