Catching an exception while using a Python ‘with’ statement

from __future__ import with_statement try: with open( “a.txt” ) as f : print f.readlines() except EnvironmentError: # parent of IOError, OSError *and* WindowsError where available print ‘oops’ If you want different handling for errors from the open call vs the working code you could do: try: f = open(‘foo.txt’) except IOError: print(‘error’) else: with f: … Read more

WPF global exception handler [duplicate]

You can trap unhandled exceptions at different levels: AppDomain.CurrentDomain.UnhandledException From all threads in the AppDomain. Dispatcher.UnhandledException From a single specific UI dispatcher thread. Application.Current.DispatcherUnhandledException From the main UI dispatcher thread in your WPF application. TaskScheduler.UnobservedTaskException from within each AppDomain that uses a task scheduler for asynchronous operations. You should consider what level you need to … Read more

Why return NotImplemented instead of raising NotImplementedError

It’s because __lt__() and related comparison methods are quite commonly used indirectly in list sorts and such. Sometimes the algorithm will choose to try another way or pick a default winner. Raising an exception would break out of the sort unless caught, whereas NotImplemented doesn’t get raised and can be used in further tests. http://jcalderone.livejournal.com/32837.html … Read more

ASP.NET Core Web API exception handling

Quick and Easy Exception Handling Simply add this middleware before ASP.NET routing into your middleware registrations. app.UseExceptionHandler(c => c.Run(async context => { var exception = context.Features .Get<IExceptionHandlerPathFeature>() .Error; var response = new { error = exception.Message }; await context.Response.WriteAsJsonAsync(response); })); app.UseMvc(); // or .UseRouting() or .UseEndpoints() Done! Enable Dependency Injection for logging and other purposes … Read more

Why is “except: pass” a bad programming practice?

As you correctly guessed, there are two sides to it: Catching any error by specifying no exception type after except, and simply passing it without taking any action. My explanation is “a bit” longer—so tl;dr it breaks down to this: Don’t catch any error. Always specify which exceptions you are prepared to recover from and … Read more

Cannot delete directory with Directory.Delete(path, true)

Editor’s note: Although this answer contains some useful information, it is factually incorrect about the workings of Directory.Delete. Please read the comments for this answer, and other answers to this question. I ran into this problem before. The root of the problem is that this function does not delete files that are within the directory … Read more

Difference between using Throwable and Exception in a try catch [duplicate]

By catching Throwable it includes things that subclass Error. You should generally not do that, except perhaps at the very highest “catch all” level of a thread where you want to log or otherwise handle absolutely everything that can go wrong. It would be more typical in a framework type application (for example an application … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)