First, make sure that your project is allowing you to see files that are not "included" in your project.
Second, look through your folders and find any files with an exclamation point before the filename. Fix the missing or renamed file problem. Normally this involves restoring the previous file name or moving files back into the old directory or removing unused files. Try rebuilding and you should be fine!
Happy Coding....... :)
Thursday, February 25, 2010
Monday, February 15, 2010
Rethrowing exceptions and preserving the full call stack trace
Did you know that depending on the way you rethrow exceptions you may lose important information? There are already several blog posts that explain and demonstrate the difference between throw and throw ex. I'm realizing only now that none of the two solutions yields the complete call stack trace information!
Let's see what the problem is and I'll show you the real solution.
I'll use the following method to generate an exception:
private static void BadWork(){
int i = 0;
int j = 12 / i;
// Line 10: DivideByZeroException
int k = j + 1;}
Let's consider what happens if we call BadWork and rethrow the exception with throw ex as follows:
try{
BadWork();}
catch (Exception ex){
// do something // ...
throw ex; // Line 24}
Here is the call stack trace that we get in this case:
Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero. at Program.WithThrowEx() in Program.cs:line 24 at Program.Main(String[] args) in Program.cs:line 88
Line 24 is where throw ex is, not where the exception was thrown.
Let's now replace throw ex by throw:
try{
BadWork();}
catch{ // do something // ...
throw; // Line 38}This time, here is the call stack trace:
Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero. at Program.BadWork() in Program.cs:line 10 at Program.WithThrow() in Program.cs:line 38 at Program.Main(String[] args) in Program.cs:line 89
As you can see, we get one additional stack frame this time. Line 10 is where the exception was thrown, which is important information because this is the only information that identifies where the exception actually happened.
This shows that it's better to use throw rather than throw ex if you want the full stack trace information to be preserved. However, there are cases where throw is not enough. In the following example, throw does not preserve the full stack trace:
try{
int i = 0;
int j = 12 / i; // Line 47
int k = j + 1;}
catch{ // do something // ...
throw; // Line 54}
Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero. at Program.WithThrowIncomplete() in Program.cs:line 54 at Program.Main(String[] args) in Program.cs:line 106
This time, you can see that information is lost again. Line 54 is where throw is, not where the exception was thrown.
To preserve the full call stack information, you need to use the following method:
private static void PreserveStackTrace(Exception exception){ MethodInfo preserveStackTrace = typeof(Exception).GetMethod("InternalPreserveStackTrace", BindingFlags.Instance BindingFlags.NonPublic); preserveStackTrace.Invoke(exception, null);}
This method can be used as follows:
try{ int i = 0;
int j = 12 / i; // Line 78
int k = j + 1;
}
catch (Exception ex)
{ // do something // ...
PreserveStackTrace(ex);
throw; // Line 86
}
Here is the new call stack information you get with the above code:Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero. at Program.WithThrowAndStackTracePreservation() in Program.cs:line 78 at Program.WithThrowAndStackTracePreservation() in Program.cs:line 86 at Program.Main(String[] args) in Program.cs:line 110
Here is the call stack information you get with throw ex and a call to PreserveStackTrace:
Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero. at Program.BadWork() in Program.cs:line 10
at Program.WithThrowExAndStackTracePreservation() in Program.cs:line 62 at Program.WithThrowExAndStackTracePreservation() in Program.cs:line 69
at Program.Main(String[] args) in Program.cs:line 109
Here we get the full call stack information. Lines 78 and 10 are where the exceptions were thrown. To my knowledge, this is the only way to get complete call stack information in your logs. Without it, it may be difficult to hunt down some bugs.It's worth noting that if you call PreserveStackTrace, then you can use throw or throw ex and you'll equally get the full stack trace information.
Let's see what the problem is and I'll show you the real solution.
I'll use the following method to generate an exception:
private static void BadWork(){
int i = 0;
int j = 12 / i;
// Line 10: DivideByZeroException
int k = j + 1;}
Let's consider what happens if we call BadWork and rethrow the exception with throw ex as follows:
try{
BadWork();}
catch (Exception ex){
// do something // ...
throw ex; // Line 24}
Here is the call stack trace that we get in this case:
Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero. at Program.WithThrowEx() in Program.cs:line 24 at Program.Main(String[] args) in Program.cs:line 88
Line 24 is where throw ex is, not where the exception was thrown.
Let's now replace throw ex by throw:
try{
BadWork();}
catch{ // do something // ...
throw; // Line 38}This time, here is the call stack trace:
Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero. at Program.BadWork() in Program.cs:line 10 at Program.WithThrow() in Program.cs:line 38 at Program.Main(String[] args) in Program.cs:line 89
As you can see, we get one additional stack frame this time. Line 10 is where the exception was thrown, which is important information because this is the only information that identifies where the exception actually happened.
This shows that it's better to use throw rather than throw ex if you want the full stack trace information to be preserved. However, there are cases where throw is not enough. In the following example, throw does not preserve the full stack trace:
try{
int i = 0;
int j = 12 / i; // Line 47
int k = j + 1;}
catch{ // do something // ...
throw; // Line 54}
Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero. at Program.WithThrowIncomplete() in Program.cs:line 54 at Program.Main(String[] args) in Program.cs:line 106
This time, you can see that information is lost again. Line 54 is where throw is, not where the exception was thrown.
To preserve the full call stack information, you need to use the following method:
private static void PreserveStackTrace(Exception exception){ MethodInfo preserveStackTrace = typeof(Exception).GetMethod("InternalPreserveStackTrace", BindingFlags.Instance BindingFlags.NonPublic); preserveStackTrace.Invoke(exception, null);}
This method can be used as follows:
try{ int i = 0;
int j = 12 / i; // Line 78
int k = j + 1;
}
catch (Exception ex)
{ // do something // ...
PreserveStackTrace(ex);
throw; // Line 86
}
Here is the new call stack information you get with the above code:Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero. at Program.WithThrowAndStackTracePreservation() in Program.cs:line 78 at Program.WithThrowAndStackTracePreservation() in Program.cs:line 86 at Program.Main(String[] args) in Program.cs:line 110
Here is the call stack information you get with throw ex and a call to PreserveStackTrace:
Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero. at Program.BadWork() in Program.cs:line 10
at Program.WithThrowExAndStackTracePreservation() in Program.cs:line 62 at Program.WithThrowExAndStackTracePreservation() in Program.cs:line 69
at Program.Main(String[] args) in Program.cs:line 109
Here we get the full call stack information. Lines 78 and 10 are where the exceptions were thrown. To my knowledge, this is the only way to get complete call stack information in your logs. Without it, it may be difficult to hunt down some bugs.It's worth noting that if you call PreserveStackTrace, then you can use throw or throw ex and you'll equally get the full stack trace information.
Index Scan Vs Index Seek
Index Scan retrieves all the rows from the table. Index Seek retrieves selective rows from the table.
Index Scan: Since a scan touches every row in the table whether or not it qualifies, the cost is proportional to the total number of rows in the table. Thus, a scan is an efficient strategy if the table is small or if most of the rows qualify for the predicate.
Index Seek: Since a seek only touches rows that qualify and pages that contain these qualifying rows, the cost is proportional to the number of qualifying rows and pages rather than to the total number of rows in the table.
Index Scan is nothing but scanning on the data pages from the first page to the last page. If there is an index on a table, and if the query is touching a larger amount of data, which means the query is retrieving more than 50 percent or 90 percent of the data, and then optimizer would just scan all the data pages to retrieve the data rows. If there is no index, then you might see a Table Scan (Index Scan) in the execution plan.
Index seeks are generally preferred for the highly selective queries. What that means is that the query is just requesting a fewer number of rows or just retrieving the other 10 (some documents says 15 percent) of the rows of the table.
Index Scan: Since a scan touches every row in the table whether or not it qualifies, the cost is proportional to the total number of rows in the table. Thus, a scan is an efficient strategy if the table is small or if most of the rows qualify for the predicate.
Index Seek: Since a seek only touches rows that qualify and pages that contain these qualifying rows, the cost is proportional to the number of qualifying rows and pages rather than to the total number of rows in the table.
Index Scan is nothing but scanning on the data pages from the first page to the last page. If there is an index on a table, and if the query is touching a larger amount of data, which means the query is retrieving more than 50 percent or 90 percent of the data, and then optimizer would just scan all the data pages to retrieve the data rows. If there is no index, then you might see a Table Scan (Index Scan) in the execution plan.
Index seeks are generally preferred for the highly selective queries. What that means is that the query is just requesting a fewer number of rows or just retrieving the other 10 (some documents says 15 percent) of the rows of the table.
Sunday, February 14, 2010
Find Busiest Database
You can find the busiest DB, by running the below query, In SSMS instance.
DMV sys.dm_exec_query_stats contained columns total_logical_reads, total_logical_writes, sql_handle. Column sql_handle can help to to determine the original query by CROSS JOINing DMF sys.dm_exec_sql_text. From DMF sys.dm_exec_sql_text Database ID and from Database ID can be figured out very quickly.
SELECT SUM(deqs.total_logical_reads) TotalPageReads,
SUM(deqs.total_logical_writes) TotalPageWrites,
CASE
WHEN DB_NAME(dest.dbid) IS NULL THEN 'AdhocSQL'
ELSE DB_NAME(dest.dbid) END Databasename
FROM sys.dm_exec_query_stats deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
GROUP BY DB_NAME(dest.dbid)
DMV sys.dm_exec_query_stats contained columns total_logical_reads, total_logical_writes, sql_handle. Column sql_handle can help to to determine the original query by CROSS JOINing DMF sys.dm_exec_sql_text. From DMF sys.dm_exec_sql_text Database ID and from Database ID can be figured out very quickly.
SELECT SUM(deqs.total_logical_reads) TotalPageReads,
SUM(deqs.total_logical_writes) TotalPageWrites,
CASE
WHEN DB_NAME(dest.dbid) IS NULL THEN 'AdhocSQL'
ELSE DB_NAME(dest.dbid) END Databasename
FROM sys.dm_exec_query_stats deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
GROUP BY DB_NAME(dest.dbid)
Thursday, February 11, 2010
Missing-build-configuration-dropdown-in-visual-studio-2008
My project is using Microsoft Visual Studio 2008 for development. For some strange reason the build configuration dropdown does not show up by default in the IDE.
I found the following procedure online and very helpful. It describes how to get this useful dropdown visible:
1. In Visual Studio 2008, click “Tools” -> “Options”
2. Under “Projects and Solutions” -> “General”, check “Show advanced build options”, and click “OK”
3. Right click anywhere in the blank space of the toolbar and click “Customize…”
4. In “Commands” tab, select “Build” in “Categories”.
5. Scroll the right listbox to the bottom and drag “Solution Configurations” to the toolbar
I really can’t take credit for creating this procedure since I found it somewhere else on the web, but I had a lot of trouble re-finding the original posting in my browser history when the dropdown suddenly disappeared from my IDE today.
I found the following procedure online and very helpful. It describes how to get this useful dropdown visible:
1. In Visual Studio 2008, click “Tools” -> “Options”
2. Under “Projects and Solutions” -> “General”, check “Show advanced build options”, and click “OK”
3. Right click anywhere in the blank space of the toolbar and click “Customize…”
4. In “Commands” tab, select “Build” in “Categories”.
5. Scroll the right listbox to the bottom and drag “Solution Configurations” to the toolbar
I really can’t take credit for creating this procedure since I found it somewhere else on the web, but I had a lot of trouble re-finding the original posting in my browser history when the dropdown suddenly disappeared from my IDE today.
Visual Studio 2005/2008 missing templates
Hye,
Few days back, When I was working in vs2008. I found that all of studio templates were missing. I found a command that will install all the templates. So Instead of reinstalling vs 2005/2008 , you should run the following command.
devenv /installvstemplates in visual studio command prompt. Close all the vs instances before running this command.
It worked for me !
Few days back, When I was working in vs2008. I found that all of studio templates were missing. I found a command that will install all the templates. So Instead of reinstalling vs 2005/2008 , you should run the following command.
devenv /installvstemplates in visual studio command prompt. Close all the vs instances before running this command.
It worked for me !
Subscribe to:
Comments (Atom)