difference between exception handling and exception throwing

Posted by Jameschowdare under Regular Expressions on 2/17/2010 | Views : 3738 | Status : [Member] | Replies : 2
if we have 3 tier

how we pass / handle excpetion over the tiers.

ex:In Dal
public dataset getempid(){
try
{
}
catch(Exception ex)
{
}
}

In Busibess layer
public dataset Bgetempid()
{
// my doubt is again here i have to write try catch block
}
now in Presentation layer
we create object for the business layer
here howto catch theexception and how to knowwhere the exception has occured




Responses

Posted by: Abhi2434 on: 2/17/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
Well,

Handling an Exception means you are taking care of the exception in that particular time, so that other operation runs smoothly. Once the exception is handled, you will not find the exception again anywhere outside it.

We use Try / catch block to gracefully handle exception in the code which might through exceptions.

On the other hand, throwing an exception means you are terminating the current operation and also letting the code outside the block to stop. We generally throw exception when we think that the current exception is important enough to stop the current operation.

See, if you find the exception is a major one, throw the exception from the DLL/ BLL and finally catch that in the presentation layer. U can log the exception / show message on the exception to let the user know what happened to execute the current operation.
If you think the current exception is just a minor one and noting to take much care of, then handle that directly into the layers and execute the rest normally.

This entirely depends on what you think of the current exception.

Cheers.

www.abhisheksur.com

Jameschowdare, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 2/18/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Good Day Jameschowdare

Abhi2434 is Right. i have covered the Topic in one of my articles in CodeProject. in this article
http://www.codeproject.com/KB/vb/N-Tier_Application_VB.aspx

i explained it. in the DAL(Data Access Layer) you must trap the Exception with a Try and catch, but now we dont do anything with the Error in this layer, but pass it to the Calling Function or Method by using throw like this

try 

{
Con.Open();
cmdselect.ExecuteNonQuery();
}
catch(SQlException) //as you can see i did not create an object here
{
throw; //throw the Exception to the calling Function or method
}
Finall
{
con.Close();
}


i just Constructed a rough example. The DAL catches the Exception and Pass it to the Function or Method of the BLL and the BLL does the Same and in your Presentation Layer , that is where you will display you message. For this to be easy , you need to ask your self the purpose of this Tiers. BLL is Business Logic Layer not Error Layer. So it does not mean if an Exception has occurred it needs to be handled by the BLL.

Thank you for posting at Dotnetfunda



Thank you for posting at Dotnetfunda
[Administrator]

Jameschowdare, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response