exception-handling

What benefit does the new “Exception filter” feature provide?

瘦欲@ 提交于 2019-12-17 18:25:29
问题 C# 6 has a new feature called "exception filtering" The syntax is like this: catch (Win32Exception exception) when (exception.NativeErrorCode == 0x00042) { //Do something here } I couldn't help but wonder what the benefit of that is over the current approach: catch (Win32Exception exception) { if (exception.NativeErrorCode == 0x00042) { //Do something here } } Is it a big deal that filtering happen before the curly bracket? Perhaps in relation to performance or security? 回答1: The Exception

Using global exception handling with “setUncaughtExceptionHandler” and “Toast”

偶尔善良 提交于 2019-12-17 18:22:00
问题 I am trying to create a simple exception handler which will help me debug the application. Right now, when I have an exception I am forced to connect with Eclipse debugger merely to see the exception details. To avoid that I've used setUncaughtExceptionHandler to handle any unhandled exception and display a Toast on the exception. Unfortunately, that doesn't work. public class TicTacToe extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle

Unable to cast object of type 'System.Linq.Expressions.UnaryExpression' to type 'System.Linq.Expressions.MemberExpression'

这一生的挚爱 提交于 2019-12-17 17:57:13
问题 I created a method in C# to get methodname public string GetCorrectPropertyName<T>(Expression<Func<T, string>> expression) { return ((MemberExpression)expression.Body).Member.Name; // Failure Point } and calling it as string lcl_name = false; public string Name { get { return lcl_name ; } set { lcl_name = value; OnPropertyChanged(GetCorrectPropertyName<ThisClassName>(x => x.Name)); } } This works fine if property is string and for all other types gives this exception: Unable to cast object of

Catching specific vs. generic exceptions in c#

大城市里の小女人 提交于 2019-12-17 17:52:50
问题 This question comes from a code analysis run against an object I've created. The analysis says that I should catch a more specific exception type than just the basic Exception. Do you find yourself using just catching the generic Exception or attempting to catch a specific Exception and defaulting to a generic Exception using multiple catch blocks? One of the code chunks in question is below: internal static bool ClearFlags(string connectionString, Guid ID) { bool returnValue = false;

Why and how would you use Exceptions in this sample PHP code?

删除回忆录丶 提交于 2019-12-17 17:40:55
问题 I've been wondering why would I use Exceptions in my PHP. Let's take a look at a simple example: class Worker { public function goToWork() { return $isInThatMood ? // Okay, I'll do it. true : // In your dreams... false; } } $worker = new Worker; if (!$worker->goToWork()) { if (date('l',time()) == 'Sunday') echo "Fine, you don't have to work on Sundays..."; else echo "Get your a** back to work!"; } else echo "Good."; Is there a reason for me to use Exceptions for that kind of code? Why? How

Can I get the local variables of a Python function from which an exception was thrown?

偶尔善良 提交于 2019-12-17 17:37:24
问题 I'm writing a custom logging system for a project. If a function throws an exception, I want to log its local variables. Is it possible to access the raising function's local variables from the except block that caught the exception? For example: def myfunction(): v1 = get_a_value() raise Exception() try: myfunction() except: # can I access v1 from here? 回答1: It's generally cleaner design to pass the value to the exception, if you know that your exception handling code is going to need it.

How can I print stack trace for caught exceptions in C++ & code injection in C++

荒凉一梦 提交于 2019-12-17 17:29:33
问题 I want to have stack trace not for my exceptions only but also for any descendants of std::exception As I understand, stack trace is completely lost when exception is caught because of stack unwinding (unrolling). So the only way I see to grab it is injection of code saving context info (stack trace) at the place of std::exception constructor call. Am I right? If it is the case, please tell me how code injection can be done (if it can) in C++. Your method may be not completely safe because I

Is Virtual Inheritance necessary for Exceptions?

喜你入骨 提交于 2019-12-17 16:44:51
问题 I understand the need for virtual inheritance when using multiple inheritance -- it solves the Dreaded Diamond Problem. But what if I'm not using multiple inheritance? Is there a need for virtual inheritance at all? I seem to recall hearing that it was important for exceptions (throw a derived class, catch by base class reference). But shouldn't virtual destructors be sufficient for that? I've tried searching for the reference page I once saw on this, but I can't seem to find it. 回答1: You're

Bad idea to catch all exceptions in Python

天大地大妈咪最大 提交于 2019-12-17 16:36:04
问题 Why is it a bad idea to catch all exceptions in Python ? I understand that catching all exceptions using the except: clause will even catch the 'special' python exceptions: SystemExit , KeyboardInterrupt , and GeneratorExit . So why not just use a except Exception: clause to catch all exceptions? 回答1: Because it's terribly nonspecific and it doesn't enable you to do anything interesting with the exception. Moreover, if you're catching every exception there could be loads of exceptions that

What happens when a .NET thread throws an exception?

独自空忆成欢 提交于 2019-12-17 16:16:03
问题 We have an interface IPoller for which we have various implementations. We have a process that will take an IPoller and start it in a separate thread. I'm trying to come up with a generic way of providing exception handling for any IPollers which don't do it themselves. My original thinking was to create an implementation of IPoller that would accept an IPoller and just provide some logging functionality. The question I ran into though is how would I provide this error handling? If I have