Showing posts with label Exception Handling. Show all posts
Showing posts with label Exception Handling. Show all posts

Monday, January 2, 2012

How to get call stack programmatically?



StackTrace class is useful to get an execution call stack of running program.  StackTrace class provides information which is useful while debugging application. StackFrame class contains useful information like filename, line number and column number. StackFrame is created while execution of program on every method calls. StackFrame information will be the most useful while debugging application. StackTrace and StackFrame class are available inside System.Diagnostics namespace


Let’s have a look on below code.


public class StackTraceTest
{
    public StackTraceTest()
    {
    }

    public void method1()
    {
        method2();
    }
    public void method2()
    {
        method3();
    }
    public void method3()
    {
        try
        {
            throw new Exception("An error occured");
        }
        catch (Exception ex)
        {
            StackTrace st = new StackTrace(true);
            Console.WriteLine("Call Stack :");

            foreach (StackFrame sf in st.GetFrames())
                Console.WriteLine("File: {0}, Method: {1}, 
                    Line: {2}, Column: {3}, Offset: {4}"
                    sf.GetFileName(),
                    sf.GetMethod().Name,
                    sf.GetFileLineNumber(),
                    sf.GetFileColumnNumber(),
                    sf.GetILOffset()
                    );
        }
    }
}

public static void main()
{
    StackTraceTest stest = new StackTraceTest();
    stest.method1();
}

Output

File: StackTraceDemo.xaml.cs, Method: method3, Line: 52, Column: 13, Offset: 21
File: StackTraceDemo.xaml.cs, Method: method2, Line: 42, Column: 9, Offset: 7
File: StackTraceDemo.xaml.cs, Method: method1, Line: 38, Column: 9, Offset: 7

As per above output, StackTrace and StackFrame class are providing useful information while debugging. GetFrames method returns collection of StackFrame classes. Each stackframe instance provides information like method name, file name, line number, column number, offset etc.


Below is the snapshot of CallStack window from Visual Studio



See Also – 

Wednesday, September 28, 2011

How to handle exceptions while working with Threading?


Everybody knows how to handle exception in application using try catch and finally block. In this post I will explain how handle exception while working with Threading. Multiple threads can run inside single process parallelly. WPF and Winforms application runs on UI Thread and handled only exception which are thrown from the main thread. So when you create new Thread from application and the calling method throws an exception which is not handled by main UI Thread.

Let’s understand using simple example.

public static void Main()
{
    try
    {
        Thread thread = new Thread(new ThreadStart(DisplayName));
        thread.Start();
    }
    catch (DivideByZeroException ex) //Exception will not handled here
    {
        MessageBox.Show(ex.ToString());
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}
public void DisplayName()
{
    int a = 0;
    int b = 10 / a; //throws unhandled exception
}

As per above code snippet, method DisplayName throws DivideByZeroException but it will not handled by the try catch block written inside Main() method and application shows unhandled application. Keep in mind that each thread has its own independent execution path. Let’s have a look on below code and see how to overcome with this scenario.

public static void Main()
{
    Thread thread = new Thread(new ThreadStart(DisplayName));
    thread.Start();
}

public void DisplayName()
{
    try
    {
        int a = 0;
        int b = 10 / a;
    }
    catch (DivideByZeroException ex)
    {
        MessageBox.Show(ex.ToString());
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

In above code, exception is handled on DisplayName method and user can see the correct error message box showing exception information. So now exception is handled within or inside thread execution path and this is the effective way to handling exception with threading.

Hope you liked this tip and also know correct way to handle exception while working with threading.

See also -