Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net: debugger highlighting line of code not actually being executed

Tags:

c#

Copy and paste the following into a new console application in VS. Add references to System.Web and System.Web.Services (I know console apps don't need these assemblies, I'm just showing you a snippet of code that does not work in my web application).

Although both conditions in the if statement are false, it's turning out to be true. Anyone know the reason why? (Visual Studio 2008 9.0.30729.1) .NET 3.5 SP1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services;
using System.Web;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            string x = "qweqweqw";
            string y =
                "{\"textMedia\":[-1,-1,-1,-1,-1],\"textOperand\":[1,1,1,1,1],\"textString\":[\"\",\"\",\"\",\"\",\"\"],\"dateSite\":[-11],\"dateOperand\":[],\"dateString\":[],\"status\":[-11,0,0],\"media\":[-11,0,0],\"subItem\":true,\"context\":false,\"branchSearch\":false,\"profileIDs\":[2,5,18],\"profileViewIDs\":[48,58,38],\"currentSelectedBranch\":0}";

            SaveSearch(x, y);
        }

        [WebMethod]
        public static object SaveSearch(string name, string encodedSearch)
        {
            object response = new { };

            string x = name;
            string y = encodedSearch;

            // Why does this if statement throw an exception if both equal false?
            if (x.Trim().Equals(string.Empty) || y.Trim().Equals(string.Empty))
                throw new AjaxErrorException("Save Search", "Something went wrong", "JSFunction");


            try
            {
                {
                    return new
                    {
                        error = false,
                        name = name,
                        userID = 123,
                        date = DateTime.Now
                    };
                }
            }
            catch (Exception ex)
            {
                String e;

                if (HttpContext.Current.IsDebuggingEnabled)
                    e = ex.Message + "\n\n" + ex.StackTrace;
                else
                    e = "error error aliens approaching";

                throw new AjaxErrorException("Save Search", e, "");
            }

            return response;
        }

        public class AjaxErrorException : System.Exception
        {
            public AjaxErrorException(string title, string details, string function)
                : base(title)
            { }
            string _stackTrace;
            public override string StackTrace
            {
                get
                {
                    return _stackTrace;
                }
            }
        }
    }
}
like image 456
Razor Avatar asked Mar 10 '10 23:03

Razor


2 Answers

I actually check and although the debugger step into the statement following the if (throw statement), it is not actually throw the exception. I suspect it is the inconsistency between IDE, IL generated and Debugger particularly for throw statement. If you try other type of statement, you dont actually see the problem. It seems to related to this post too If statement weirdness in Visual Studio 2008

I inserted the assert statement inside the if block like below to ensure there was no assertion triggered.

System.Diagnostics.Debug.Assert(false);
like image 97
Fadrian Sudaman Avatar answered Oct 15 '22 17:10

Fadrian Sudaman


So I tried pasting this exact code into a new console application in vs2008 and adding the necessary references. Here's what it did for me:

Running the application did not seem to throw any exception. However, when I ran it in the debugger and stepped over line 30 (the line with the if), the debugger highlighted the net line (with the throw) in yellow. I was able to keep stepping over code with no actual exception being thrown. If I set a breakpoint on line 31 (the throw line) it never gets hit.

I think the debugger UI is simply off. It does not appear to actually be executing that code. I don't know what would cause that, but it does not seem to be anything to worry about.

like image 40
David Hogue Avatar answered Oct 15 '22 19:10

David Hogue