exception-handling

Exception Handling in ASP.NET Web Api 2

Deadly 提交于 2019-12-09 10:51:05
问题 Problem: I need to handle web api 2 exceptions and return a rich object with the correct status code (401 for Unauthorized, 404 for ContentNotFound, etc) and some extra information as the content. Moreover, I need the content to look like a serialized Exception object (have the message , exceptionMessage , stackTrace , ... properties). Suggested Solutions: Create custom exception classes and writing a custom exception filter to apply to any controller's action. this custom exception filters

Scala continuation and exception handling

此生再无相见时 提交于 2019-12-09 10:11:52
问题 Suppose, I would like to catch an exception, fix the problem caused the exception and return to the same execution point where the exception occurred to continue. How can I implement it with continuations in Scala? Does it make any sense? 回答1: Here is one of the possible ways of implementing resumable error handling: import java.io.File import java.lang.IllegalStateException import scala.util.continuations._ // how it works ctry { println("start") val operationResult = someOperation(new File(

Catch Objective-C exception in Swift

牧云@^-^@ 提交于 2019-12-09 09:40:18
问题 I am trying to set the value of an @objc object in my Swift code using obj.setValue(value, forKey: key) . It works fine when the object has the property being set. But if it doesn't, my app crashes hard with an uncaught NSException ("class is not key value coding-compliant…"). How can I catch and absorb this exception like I can in Objective-C so as to not crash my app? I tried wrapping it in a Swift try-catch, but it complains that none of the instructions throws and does nothing. 回答1: see

Throwing always the same exception instance in Java

旧巷老猫 提交于 2019-12-09 09:07:04
问题 It was always told me that Java exception handling is quite expensive. I'm asking if is it a good practice creating an exception instance of a specific type at the beginning of the program and without creating a new one, throwing always the same exception object. I just want to make an example. Common code: if (!checkSomething(myObject)) throw new CustomException("your object is invalid"); alternative: static CustomException MYEXP = new CustomException("your object is invalid"); //somewhere

Why Spring MVC does not allow to expose Model or BindingResult to an @ExceptionHandler?

浪尽此生 提交于 2019-12-09 08:33:53
问题 Situation I'm trying to group the code that logs the exceptions and render a nice view in a few methods. At the moment the logic is sometime in the @RequestHandler itself (in the a catch block), othertimes is delegated to an utility class (that works but moves the logic away from the place where the exception is thrown). Spring's @ExceptionHandler seemed the way to group everything in one place (the controller itself or a parent) and get rid of some code (no need to put logic in the try-catch

Why can I not catch a Queue.Empty exception from a multiprocessing Queue?

风流意气都作罢 提交于 2019-12-09 08:18:03
问题 I'm trying to catch a Queue.Empty exception that is raised if a multiprocessing.Queue is empty. The following does not work: import multiprocessing f = multiprocessing.Queue() try: f.get(True,0.1) except Queue.Empty: print 'foo' This gives me a name error: NameError: name 'Queue' is not defined replacing Queue.Empty with multiprocessing.Queue.Empty does not help either. In this case it gives me a "AttributeError: 'function' object has no attribute 'Empty'" exception. 回答1: The Empty exception

How to catch/observe an unhandled exception thrown from a Task

隐身守侯 提交于 2019-12-09 07:53:58
问题 I'm trying to log / report all unhandled exceptions in my app (error reporting solution). I've come across a scenario that is always unhandled. I'm wondering how would I catch this error in an unhandled manner. Please note that I've done a ton of research this morning and tried a lot of things.. Yes, I've seen this, this and many more. I'm just looking for a generic solution to log unhandled exceptions. I have the following code inside of a console test apps main method: Task.Factory.StartNew

how to: handle exceptions, best practices

风格不统一 提交于 2019-12-09 07:48:04
问题 need to implement a global error handling, so maybe you can help out with the following example... i have this code: public bool IsUserAuthorizedToSignIn(string userEMailAddress, string userPassword) { // get MD5 hash for use in the LINQ query string passwordSaltedHash = this.PasswordSaltedHash(userEMailAddress, userPassword); // check for email / password / validity using (UserManagementDataContext context = new UserManagementDataContext()) { var users = from u in context.Users where u

UnauthorizedAccessException vs SecurityException

非 Y 不嫁゛ 提交于 2019-12-09 07:46:52
问题 The MSDN constructor for a FileStream says that it may throw either an UnauthorizedAccessException or a SecurityException. Here's what MSDN says about these exceptions. UnauthorizedAccessException: The exception that is thrown when the operating system denies access because of an I/O error or a specific type of security error. SecurityException: The exception that is thrown when a security error is detected. How are these two similar exceptions different? What situations will trigger either

Exception handling in layered architecture

筅森魡賤 提交于 2019-12-09 07:36:20
问题 We are Refactoring (and of-course redesigning) our Services in layered design. We have Service operations layer (BLL), Network abstraction layer -> (deals with network proxy), Data abstraction layer. But we are a bit baffled about our exception handling strategy. We don't want to expose too much information from BLL to outside world. (from other layers to bll is fine) We don't want to clutter the code with try catch stacks We don't want to mess the exception handling code(like logging,