error-handling
How to throw exception without resetting stack trace?
With .NET Framework 4.5 there is now an ExceptionDispatchInfo which supports this exact scenario. It allows capturing a complete exception and rethrowing it from somewhere else without overwriting the contained stack trace. code sample due to request in comment using System.Runtime.ExceptionServices; class Test { private ExceptionDispatchInfo _exInfo; public void DeleteNoThrow(string path) { try { File.Delete(path); … Read more
Error handling with Mongoose
If you’re using Express, errors are typically handled either directly in your route or within an api built on top of mongoose, forwarding the error along to next. app.get(‘/tickets’, function (req, res, next) { PlaneTickets.find({}, function (err, tickets) { if (err) return next(err); // or if no tickets are found maybe if (0 === tickets.length) … Read more
Using Spring Boot’s ErrorController and Spring’s ResponseEntityExceptionHandler correctly
Spring Boot application has a default configuration for error handling – ErrorMvcAutoConfiguration. What it basically does, if no additional configuration provided: it creates default global error controller – BasicErrorController it creates default ‘error’ static view ‘Whitelabel Error Page’. BasicErrorController is wired to ‘/error’ by default. If there is no customized ‘error’ view in the application, … Read more
How to handle wrong data type input
The reason the program goes into an infinite loop is because std::cin‘s bad input flag is set due to the input failing. The thing to do is to clear that flag and discard the bad input from the input buffer. //executes loop if the input fails (e.g., no characters were read) while (std::cout << “Enter … Read more
Is it possible to use custom error pages with MVC site but not Web API?
Well, after a nearly a year of letting this question marinade, I gave it another shot. Here’s the web.config magic that got me what I wanted: <!– inside of <configuration> to allow error responses from requests to /api through –> <location path=”api”> <system.webServer> <validation validateIntegratedModeConfiguration=”false” /> <httpErrors errorMode=”DetailedLocalOnly” existingResponse=”PassThrough” > <clear/> </httpErrors> </system.webServer> </location> <!– … Read more
Why would you ever use “On Error Goto 0”?
In VB6, you can specify that you want errors to be handled by particular code later in the routine: Sub Bar() On Error Goto MyHandler … …some code that throws an error… … Exit Sub MyHandler: …some error handler code (maybe pops up a dialog) End Sub It may be the case, however, that the … Read more
Ignore Unparseable JSON with jq
Assuming that each log entry is exactly one line, you can use the -R or –raw-input option to tell jq to leave the lines unparsed, after which you can prepend fromjson? | to your filter to make jq try to parse each line as JSON and throw away the ones that error.
Getting exception details in Python
You can use sys.exc_info to get information about the exception currently being handled, including the exception object itself. An IOError exception contains all of the information you need, including the filename, the errno, and a string describing the error: import sys try: f1 = open(‘example1’) f2 = open(‘example2’) except IOError: type, value, traceback = sys.exc_info() … Read more