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.