[RESOLVED] Exception Issue and N-tier issue (c#)
Hi,
I was required to build a N-tier system.
User -> Business -> Data -> Database
Interface <- layer <- Access <-
My question is, how do u implement the exception in the data-access-layer and show it to the user-interface layer.
The following is my code from the data-access layer in c#
Code:
public class DataAccess
{
public string ReadData()
{
try
{
// Read data from the database
}
catch (Exception e)
{
throw new IOException ("Could not read from database", e);
/* I can just retun null here, but I just want to show the message to
the user
*/
}
}
}
Thanks a lot. :)
Re: Exception Issue and N-tier issue (c#)
User -> Business -> Data -> Database
Interface <- layer <- Access <-
They should be
User Interface <-> Business layer <-> Data access <-> Database
Re: Exception Issue and N-tier issue (c#)
In your Data Access Class just throw your exception. Like this:
Code:
catch (Exception e)
{
throw new IOException ("Could not read from database. Details: " + e.Message, e);
}
In your BusinessLayer you catch Exceptions again and throw it again:
Code:
catch IOException(Exception e)
{
throw new Exception ("Error in my Class-myFunction. Details:" + e.Message, e);
}
And in your User Interface catch the error again and display the Information you want to show the user. Since you always including the original Exception in your new Exceptions you have control over the whole Stack Trace. Bubbling your Exception all the way up to your user-interface makes sense since all of your other layers dont know what Information you actually want to present.
In your User-Interface layer you can then format your Exception nicely and present the Error Information you want.
Of course catching Exceptions is expensive in .NET but still I like this Method the most!
HTH,
Stephan
Re: Exception Issue and N-tier issue (c#)
Oh and by the way, I usually create my Own Exceptions for a lot of classes I use. For example my Own DataAccessException. So if an error occures in my Data Access Class I catch that and throw a new DataAccessException. In my BusinessLayer I can then watch for that specific Exception and handle it accordingly. For example retry, or similar!
Stephan
Re: Exception Issue and N-tier issue (c#)
To Stephan,
Just wondering how can u catch the exception in the user-interface ???
Thanks a lot. I am so virgin to this programming :)
Re: Exception Issue and N-tier issue (c#)
OK this is just a quick and dirty example. Havnt tested this:
In your DataLayer do something like this:
Code:
public static string doSomething
{
int b = 10;
int c = 0;
try
{
/* Divide by Zero to force an Exception */
int a = b /c;
return a.ToString();
}
catch(Exception ex)
{
throw new Exception("Error in blah" + ex.Message,ex);
}
}
This method will throw an Exception.
In your Business Layer oyu call the Method and catch an Exception like this:
Code:
public static string doSomething()
{
try
{
return DataLayer.doSoemthing();
}
catch(Exception ex)
{
throw new Exception("Error in blah" + ex.Message,ex);
}
}
And finally in your UserInterface do something like this:
Code:
try
{
txtMyResult = BusinessLayer.doSomething();
}
catch(Exception ex)
{
txtMyResult = ex.Message;
}
Again this is just some quick and dirty example. And it might be a good idea to create your own Exception classes. You are more variable in your Error handling then. If you have any more questions just post!
Stephan
Re: Exception Issue and N-tier issue (c#)
Cheers Stephan,
You're a legend.
Re: [RESOLVED] Exception Issue and N-tier issue (c#)
Ehm.. Stephan
Do u mind posting some coding of yours in your own exception class ?? Sorry, for being such a pain....
Re: [RESOLVED] Exception Issue and N-tier issue (c#)
Thsi is a good reading for Exceptions. I'll post some code tomorrow! Not at work Today!
Edit forgot the link:
Exception Handling
Re: [RESOLVED] Exception Issue and N-tier issue (c#)
Hi Stephan,
Thanks for the link :)
I am an exception-master to be after reading them
cheers