|
-
Sep 10th, 2005, 12:06 PM
#1
Thread Starter
Junior Member
[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.
-
Sep 10th, 2005, 12:08 PM
#2
Thread Starter
Junior Member
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
-
Sep 12th, 2005, 08:57 AM
#3
Hyperactive Member
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
Keep Smiling - even if its hard 
Frankie Says Relax, wossname Says Yeah!
wossname:--Currently I'm wearing a gimp suit and a parachute.
C# - Base64 Blog
-
Sep 12th, 2005, 09:01 AM
#4
Hyperactive Member
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
Keep Smiling - even if its hard 
Frankie Says Relax, wossname Says Yeah!
wossname:--Currently I'm wearing a gimp suit and a parachute.
C# - Base64 Blog
-
Sep 14th, 2005, 09:30 AM
#5
Thread Starter
Junior Member
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
-
Sep 14th, 2005, 10:26 AM
#6
Hyperactive Member
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
Keep Smiling - even if its hard 
Frankie Says Relax, wossname Says Yeah!
wossname:--Currently I'm wearing a gimp suit and a parachute.
C# - Base64 Blog
-
Sep 14th, 2005, 09:23 PM
#7
Thread Starter
Junior Member
Re: Exception Issue and N-tier issue (c#)
Cheers Stephan,
You're a legend.
-
Sep 15th, 2005, 01:46 AM
#8
Thread Starter
Junior Member
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....
-
Sep 15th, 2005, 03:22 AM
#9
Hyperactive Member
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
Last edited by Sgt-Peppa; Sep 15th, 2005 at 03:23 AM.
Reason: Forgot the link ;)
Keep Smiling - even if its hard 
Frankie Says Relax, wossname Says Yeah!
wossname:--Currently I'm wearing a gimp suit and a parachute.
C# - Base64 Blog
-
Sep 16th, 2005, 05:36 AM
#10
Thread Starter
Junior Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|