exception-handling

Are fail-fast and fail-safe exception handling principles incompatible?

人走茶凉 提交于 2019-12-05 03:32:05
问题 I'd like to understand better what is fail-fast and fail-safe. What it seems to me at first glance is that fail-fast means that we want to make the system clearly fail when any unexpected thing happens. I mean for exemple if a factory can't create an instance of object, for fail-fast principle, we really don't want the factory to return null, or empty object, or partially initialized object that could, by chance, be used correctly by the application -> most time we would have an unexpected

Django - catch exception

被刻印的时光 ゝ 提交于 2019-12-05 03:29:48
It might be a Python newbie question... try: #do something except: raise Exception('XYZ has gone wrong...') Even with DEBUG=True, I don't want this raise Exception gives me that yellow page. I actually want to handle the exception by redirecting users to an error page or shows the error (give a CSS error message on the top of the page...) How do I handle that? Can someone guide me? If I simply raise it, I will get yellow debug page (again, I don't want certain exceptions to stop the site from functioning by showing the debug page when DEBUG=True). How do I handle these exceptions in views.py?

When is KeyboardInterrupt raised in Python?

不羁的心 提交于 2019-12-05 03:28:38
All the docs tell us is, Raised when the user hits the interrupt key (normally Control-C or Delete ). During execution, a check for interrupts is made regularly. But from the point of the code, when can I see this exception? Does it occur during statement execution? Only between statements? Can it happen in the middle of an expression? For example: file_ = open('foo') # <-- can a KeyboardInterrupt be raised here, after the successful # completion of open but prior to the try? --> try: # try some things with file_ finally: # cleanup Will this code leak during a well-timed KeyboardInterrupt ? Or

check if network drive exists with timeout in c#

99封情书 提交于 2019-12-05 03:25:01
问题 i created a windows service that move files around between the server's hard drive (where the service is installed) to the network drive mapped in the server. one of the problems I encountered while creating a solution was network problems. how do i check if a network drive exists while setting a timeout in checking it? If it times out, I catch the exception and retry in X number of minutes and leave items in queue. thanks! 回答1: Put the call in a seperate thread and close the thread after a

How to distinguish programmatically between different IOExceptions?

独自空忆成欢 提交于 2019-12-05 03:22:37
I am doing some exception handling for code which is writing to the StandardInput stream of a Process object. The Process is kind of like the unix head command; it reads only part of it's input stream. When the process dies, the writing thread fails with: IOException The pipe has been ended. (Exception from HRESULT: 0x8007006D) I would like to catch this exception and let it fail gracefully, since this is expected behavior. However, it's not obvious to me how this can robustly be distinguished from other IOExceptions. I could use message, but it's my understanding that these are localized and

Should exception messages that are targeted to the developer be localized?

旧街凉风 提交于 2019-12-05 03:11:57
I am referring to exception messages that show the developer is incorrectly using an API. For example incorrectly passing a null to method. So the type of exception that the developer will get the first time they have run their incorrect code. The type of exception message that should never get to be displayed to the user of a system. This is kind of related to the theory that since the programming language is in English then the programmer already has an understanding of English. Or at least enough to decipher an exception message. http://www.codinghorror.com/blog/archives/001248.html (please

Is there a way to Wait for a TPL Task without in throwing an exception?

走远了吗. 提交于 2019-12-05 03:03:58
Some of us prefer to code in an exception-light style. However, if you wait for a Task Parallel Library task, and the task threw an exception, it will throw an exception on the calling thread as well. Is there a (preferably standard) way to avoid this behaviour and just check the response for exceptions when you get it back? You can use Task.WaitAny like: var task = Task.Run(() => { // ... throw new Exception("Blah"); }); Task.WaitAny(task); if (task.IsFaulted) { var error = task.Exception; // ... } else if (task.IsCanceled) { // ... } else { // Success } Unfortunately, this functionality is

JSLint complaining about my try/catch

纵然是瞬间 提交于 2019-12-05 03:01:01
The javascript, when run through JSLint yells at me and I am not sure why. /*jslint browser: true, devel: true, evil: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, newcap: true, immed: true */ var foo = function() { try { console.log('foo'); } catch(e) { alert(e); } try { console.log('bar'); } catch(e) { alert(e); } }; foo(); It tells me: Problem at line 12 character 11: 'e' is already defined. } catch(e) { It appears to be upset that I have a second catch(e) . Why would this be an issue? Does it not simply set e to local variable inside the catch block? Do I

Exception vs ad-hoc type. What is likely to fall over?

孤人 提交于 2019-12-05 02:55:42
问题 I'm reading a book development of enterprise applications while working on MVC 3 project. I'm currently deciding on how to handle exceptions. Previously I would let the exception bubble up the stack and then handle it at the highest layer. The book suggests to create an ad-hoc class in a domain model and return that. E.g. public sealed class MissingCustomer: Customer { } // On method failure return new MissingCustomer(); I can see the idea but I'm struggling to justify a need for that. Code

function try block. An interesting example

浪尽此生 提交于 2019-12-05 02:44:33
Consider the following C++ program struct str { int mem; str() try :mem(0) { throw 0; } catch(...) { } }; int main() { str inst; } The catch block works, i.e. the control reaches it, and then the program crashes. I can't understand what's wrong with it. Once the control reaches the end of the catch block of function-try-block of a constructor, the exception is automatically rethrown. As you don't catch it further in main(), terminate() is called. Here is an interesting reading: http://www.drdobbs.com/184401316 来源: https://stackoverflow.com/questions/3888896/function-try-block-an-interesting