3 important properties of Exception class

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 5285 red flag

In this article we will discuss 3 very important property of Exception class in C#

3 important properties of Exception class

Here we will discuss three very important properties of exception class. In any project development exception handling and log in is very crucial part. Log proper message and enough is very important. It will helpful to developer for next step.

Now, question is how we will log proper message about information! We have to extract proper message and information from exception object by setting few properties. Here we will discuss three important properties one by one.

Message property to get message from exception location

Message property will supply message from exception location. We can log this information to know exact message regarding exception. Here is sample code to do same.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using PIHelper;
using System.Data.Common;
 
namespace WindowsForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
         
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
          // Exception handeling
                try
                {
                    throw new Exception("My created exception");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
        }
       
    }
}

 

Here is sample output:


Stack Trace to detect original location of exception

Stack tracing is very important when one function call to another any exception comes from chain of function calling. For example function A is calling to function B again function B is calling to function C . Now, if C throws exception and we want to detect it from function A then we have to use StackTrace property of Exception class. Here is sample code for example.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using PIHelper;
using System.Data.Common;
 
namespace WindowsForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
         
        }
 
        private void button1_Click_1(object sender, EventArgs e)
        {
          // Exception handeling
 
            try
            {
                throw new Exception("My created exception");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }
 
 
        }
       
    }
}

In output screen we are seeing actual location of exception.


Inner exception to get information of original exception object

In conductive function calling scenario if any inner function throws exception it may replace by other exception when it re throws by different catch block.

And if we want to get information about original exception object we have to use InnerException property of Exception class. Here in sample code and implementation.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using PIHelper;
using System.Data.Common;
 
namespace WindowsForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
         
        }
 
        public void MakeException()
        {
            try
            {
                throw new ApplicationException("My created exception");
            }
            catch (Exception mainE)
            {
                throw new Exception("Exception String",mainE);
            }
        }
 
        private void button1_Click_1(object sender, EventArgs e)
        {
          // Exception handeling
 
            try
            {
                MakeException();
            }
            catch (Exception ex)
            {
MessageBox.Show(ex.InnerException.Message.ToString());
            }
        }
       
    }
}

 

Here is sample output.


 


 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Posted by: Mbksp2013 on: 8/5/2013 | Points: 25
Hi Sourav,

May i know the need of Using PIHelper namespace.
Posted by: Sourav.Kayal on: 8/5/2013 | Points: 25
Dear reader,
Here we are not using PIHelper reader namespace at all. Actually it's for platform independent data access operation. I had used it for another example. In this example it's not at all needed.

Login to post response

Comment using Facebook(Author doesn't get notification)