custom-exceptions

Python: Maximum recursion depth exceeded when printing custom exception

a 夏天 提交于 2019-12-05 02:45:25
The following code throws RuntimeError: maximum recursion depth exceeded while getting the str of an object . I can resolve the infinite recursion in two different ways, but I don't understand why each fix works and thus don't know which to use, or if either are correct. class FileError( Exception ): def __init__( self, filename=None, *a, **k ): #Fix 1: remove super super( FileError, self ).__init__( self, *a, **k ) self.filename = filename def __repr__( self ): return "<{0} ({1})>".format( self.__class__.__name__, self.filename ) #Fix 2: explicitly define __str__ #__str__ = __repr__ print(

Can you throw an array instead of a string as an exception in php?

孤街浪徒 提交于 2019-12-04 16:33:35
问题 I want to throw an array as an exception in php, instead of a string. Is it possible to do this if you define your own class that extends the Exception class? For example throw new CustomException('string', $options = array('params')); 回答1: Sure. It will just be up to your error handling code to be aware of, and make use of the array property appropriately. You can define your custom exception class's constructor to take any parameters you want, and then just be sure to call the base class's

How do I make config.exceptions_app work with rspec

╄→尐↘猪︶ㄣ 提交于 2019-12-04 08:20:21
In my Rails 3.2 app, I'm trying to use config.exceptions_app to route exceptions through the routing table to render error-specific pages (especially one for 401 Forbidden). Here's what I've got so far for configuration: # application.rb config.action_dispatch.rescue_responses.merge!('Error::Forbidden' => :forbidden) config.exceptions_app = ->(env) { ErrorsController.action(:show).call(env) } # development.rb config.consider_all_requests_local = false # test.rb config.consider_all_requests_local = false And now the meat of the matter: module Error class Forbidden < StandardError end end class

Using mockito to test methods which throw uncaught custom exceptions

末鹿安然 提交于 2019-12-03 22:08:26
How do I write a Mockito-based JUnit method to test this method adduser() ? I tried writing one, but it's failing with an error message saying exception is not handled. The error is displayed for: when(service.addUser("nginx")).thenReturn("apache"); Assume addUser() method in business class never catches any exception and rethrowing is not done. class Business { public User addUser() throws ServiceException{ User user = service.addUser("nginx"); return user; } } TEST CASE METHOD : Here in the test class I am mocking the service layer class with @Mock attribute and injecting it. @Mock Service

Can you throw an array instead of a string as an exception in php?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 09:46:52
I want to throw an array as an exception in php, instead of a string. Is it possible to do this if you define your own class that extends the Exception class? For example throw new CustomException('string', $options = array('params')); echo Sure. It will just be up to your error handling code to be aware of, and make use of the array property appropriately. You can define your custom exception class's constructor to take any parameters you want, and then just be sure to call the base class's constructor from within the constructor definition, eg: class CustomException extends \Exception {

Catch Multiple Custom Exceptions? - C++

夙愿已清 提交于 2019-12-03 06:28:34
问题 I'm a student in my first C++ programming class, and I'm working on a project where we have to create multiple custom exception classes, and then in one of our event handlers, use a try/catch block to handle them appropriately. My question is: How do I catch my multiple custom exceptions in my try/catch block? GetMessage() is a custom method in my exception classes that returns the exception explanation as a std::string . Below I've included all the relevant code from my project. Thanks for

Catch Multiple Custom Exceptions? - C++

孤街醉人 提交于 2019-12-02 19:59:02
I'm a student in my first C++ programming class, and I'm working on a project where we have to create multiple custom exception classes, and then in one of our event handlers, use a try/catch block to handle them appropriately. My question is: How do I catch my multiple custom exceptions in my try/catch block? GetMessage() is a custom method in my exception classes that returns the exception explanation as a std::string . Below I've included all the relevant code from my project. Thanks for your help! try/catch block // This is in one of my event handlers, newEnd is a wxTextCtrl try { first

User defined exceptions: when do we use them? What is an “exceptional” situation?

不问归期 提交于 2019-12-01 19:28:48
I'm sure that this question has been addressed in many best practices books, but still... Most of the times I see examples of wrong usage of custom exceptions, therefore I was wondering what would be a good situation to use them? In particular, at the moment I'm working on a type checker for a compilers course.Therefore, I have a SymbolTable class, which is rather similar to a Map.The key difference from your ordinary map is that each symbol has to be defined at most once, so a put(String, Object) operation should fail if the key we're trying to insert is already present in the SymbolTable. So

Wrap jQuery's $.ajax() method to define global error handling

拟墨画扇 提交于 2019-11-30 07:50:32
问题 Branching off of questions like this one, I'm looking to wrap jQuery's $.ajax() method such that I can provide error handling in one location, which would then be used automatically by all of an application's remote calls. The simple approach would be to simply create a new name, similar to how $.get() and $.post() implement facades to $.ajax(). However, I'm hoping to reuse the name $.ajax(), such that we can keep the rest of our code using the standard jQuery syntax, hiding from it the fact

Exception handler in Spring MVC

Deadly 提交于 2019-11-30 06:22:21
问题 I want to create an exception handler which will intercept all controllers in my project. Is that possible to do? Looks like I have to put a handler method in each controller. Thanks for your help. I have a spring controller that sends Json response. So if an exception happens I want to send an error response which can be controlled from one place. 回答1: (I found a way to implement it in Spring 3.1, this is described in the second part of this answer) See chapter 16.11 Handling exceptions of