Exception.Message vs Exception.ToString()

Exception.Message contains only the message (doh) associated with the exception. Example: Object reference not set to an instance of an object The Exception.ToString() method will give a much more verbose output, containing the exception type, the message (from before), a stack trace, and all of these things again for nested/inner exceptions. More precisely, the method … Read more

How to use ELMAH to manually log errors

Direct log writing method, working since ELMAH 1.0: try { some code } catch(Exception ex) { Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(ex)); } ELMAH 1.2 introduces a more flexible API: try { some code } catch(Exception ex) { Elmah.ErrorSignal.FromCurrentContext().Raise(ex); } There is a difference between the two solutions: Raise method applies ELMAH filtering rules to the exception. Log method … Read more

What really happens in a try { return x; } finally { x = null; } statement?

The finally statement is executed, but the return value isn’t affected. The execution order is: Code before return statement is executed Expression in return statement is evaluated finally block is executed Result evaluated in step 2 is returned Here’s a short program to demonstrate: using System; class Test { static string x; static void Main() … Read more

What happens if a finally block throws an exception?

If a finally block throws an exception what exactly happens ? That exception propagates out and up, and will (can) be handled at a higher level. Your finally block will not be completed beyond the point where the exception is thrown. If the finally block was executing during the handling of an earlier exception then … Read more

Catching multiple exception types in one catch block

Update: As of PHP 7.1, this is available. The syntax is: try { // Some code… } catch(AError | BError $e) { // Handle exceptions } catch(Exception $e) { // Handle the general case } Docs: https://www.php.net/manual/en/language.exceptions.php#example-294 RFC: https://wiki.php.net/rfc/multiple-catch Commit: https://github.com/php/php-src/commit/0aed2cc2a440e7be17552cc669d71fdd24d1204a For PHP before 7.1: Despite what these other answers say, you can catch AError … Read more

Python: One Try Multiple Except

Yes, it is possible. try: … except FirstException: handle_first_one() except SecondException: handle_second_one() except (ThirdException, FourthException, FifthException) as e: handle_either_of_3rd_4th_or_5th() except Exception: handle_all_other_exceptions() See: http://docs.python.org/tutorial/errors.html The “as” keyword is used to assign the error to a variable so that the error can be investigated more thoroughly later on in the code. Also note that the parentheses … Read more