Exception handling in
c#
In past few days I am spending some time to learn exception handling
in efficient ways. If you spent a couple of hour in net probably you will find
lot of website is talking about exception handling in programming language. But to adopt the most
efficient practice is rely challenging, basically in layered architecture.
We all are well familiar with three tier architecture , now
the question is how we will handle exception in this kind of project when we don’t
know in which layer the exception will occur?.
Ok , talking to much. Let ‘s start with very basic of
exception handling.
What is Exception ?
Let me discuss from my personal experience. Think about
those situation.
·
Trying to connect with Database . Offs !!
database server is not responding.
·
Trying to consume web service from any third
party provider.(I know you will raise complain ,if you are a paid customer, But
if not ? J
) and server is down.
Still not clear?
Ok , take another example . you are working with user’s input and suddenly
you get null value from user end !! L
And when you will try to deal with null value ,obviously it will create problem
in your application. When I was young enough in programming (still young J) we used to face those kind of situation very
often.
Ok , now it’s clear that “Exception are some kind of devil,
which will enter in room without
knocking the door J
” – making fun.
My dear reader , It’s not fun but real fact. Know one can
predict when exception will appear in application .
Solution !! ?
We have to handle exception using few block. They are
- Try
- Catch
- Finally
Try{
// Keep your error
prone code here.
}
If we think that those kind of code may create trouble in
application .simply put those within try{} block.
Catch()
This is the block where we will write exception handling
code. Now again big Question , “What is
exception handeling code ?
Let me give one example.
You are trying to connect with one database server and unfortunately it’s
not responding . Then you may try to connect with another mirror server to get
it done.
Finally
Basically , finally block are used to release the resources what
you have consumed in try block. For example dispose connect object . Dispose
object of file stream or object of image(Yes image object are highly expensive)
Let’s look a example
class Program
{
static void Main(string[] args)
{
try
{
int a = 0;
Console.Write(100 / a);
}
catch
(Exception ex)
{
Console.Write(ex.Message);
}
finally
{
Console.Write("Finally block");
}
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
try
{
int a = 0;
Console.Write(100 / a);
}
catch
(Exception ex)
{
Console.Write(ex.Message);
}
finally
{
Console.Write("Finally block");
}
Console.ReadLine();
}
}
How program flow happens here?
Within try I have kept one line of code. Hey..It’s for
simplicity dude. In real project it may need to keep hundreds of line under try
block. Now this code will throw exception and my catch block will catch it.
Within catch I am not writing anything . But you may nee to do many more thing like
1)
Log error
2)
Prepare user friendly message etc etc.
Throw your own
exception
Believe me it’s very
easy to throw your customize exception . Just write throw new Exception(" I
have thrown it");
Look below example.
static void
Main(string[] args)
{
try
{
throw new
Exception("
I have thrown it");
}
catch
(Exception ex)
{
Console.Write(ex.Message);
}
finally
{
Console.Write("Finally block");
}
Console.ReadLine();
}
Want to gather more information of Exception location and
Exception patern?
Exception object can provide you very much developer
friendly information to you .(If you are a developer ?Oh, not if , as still you are reading , means you
are developer)
Have a look of below code.
static void
Main(string[] args)
{
try
{
throw new
Exception("
I have thrown it");
}
catch
(Exception ex)
{
Console.Write("Exception Message:-
"+ ex.Message +"\n");
Console.Write("stack Trace:-
" + ex.StackTrace + "\n");
Console.Write("source:-
" + ex.Source + "\n");
}
finally
{
Console.Write("Finally block");
}
Console.ReadLine();
}
Ok . I think there is
no doubt in basic of exception handling ? Here it’s not end . it’s a vast topic to talk
. Ok in my next post I will write something regarding best way to handle
exception basically in N-Tier architecture. And will give some idea about
resource cost of exception.
No comments:
Post a Comment