473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Help with Exception handling

VMI
I have two classes in my web app: the interface and the DB connection.
In the method that connects to the DB, there's a try..catch statement
that looks like this:
public DataTable connectToDB()
{
try
{
// CONNECT ERROR. Wrong server name
}
catch(Exception ex)
{
throw new Exception("User::Login::Error occured.", ex);
}

How can I send the Exception message back to the method (in the
Interface class) that called connectToDB()? In other words, how can I
display the error in the catch{} in my webform?
Is there some type of log that automatically stores some of the error
information even if there's already code that handles it? For all
purposes, assume that I can't modify connectToDB() method because
everything's in the Production environment.

Thanks.

Sep 7 '06 #1
3 1295
Hello VMI,

Why not to wrap you call in the first class in try{}/catch{} and catch your
exception

or catch you error in the Application_Error method that is in the global.cs

VI have two classes in my web app: the interface and the DB
Vconnection.
VIn the method that connects to the DB, there's a try..catch statement
Vthat looks like this:
Vpublic DataTable connectToDB()
V{
Vtry
V{
V// CONNECT ERROR. Wrong server name
V}
Vcatch(Exception ex)
V{
Vthrow new Exception("User::Login::Error occured.", ex);
V}
VHow can I send the Exception message back to the method (in the
VInterface class) that called connectToDB()? In other words, how can I
Vdisplay the error in the catch{} in my webform?
VIs there some type of log that automatically stores some of the error
Vinformation even if there's already code that handles it? For all
Vpurposes, assume that I can't modify connectToDB() method because
Veverything's in the Production environment.
VThanks.
V>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Sep 7 '06 #2
VMI,
>I have two classes in my web app: the interface and the DB connection.
In the method that connects to the DB, there's a try..catch statement
How can I send the Exception message back to the method (in the
Interface class) that called connectToDB()?
If your code in connectToDB is throwing the new exception like this with two
constructor parameters:
throw new Exception("User::Login::Error occured.", ex);
....then you can easily get the exception that was there "before" by using
the InnerException property of the Exception object. This gives you access
to the so-called inner exception object.

An example is in order. Assume I have the following two methods:

------------------------------------
public void ExecuteSQL(string sql) {
throw new Exception("Bad SQL statement.");
}

public void ConnectToDB() {
try {
ExecuteSQL("SELECT ...");
}
catch (Exception ex)
{
throw new Exception("Connection error.",ex);
}
}

------------------------------------

Here, ExecuteSQL raises an exception that ConnectToDB catches. Also,
ConnectToDB to raises its own exception, but note how the previous exception
is passed in as an inner exception to the new exception object.

Now, if I have the following code (this is WinForms code, you would
obviously need ASP.NET code):

------------------------------------
private void button1_Click(object sender, EventArgs e) {
try {
ConnectToDB();
}
catch (Exception ex) {
MessageBox.Show("Topmost exception:\r\n" +
ex.Message + "\r\n\r\nInner exception:\r\n" +
ex.InnerException.Message);
}
}
------------------------------------

Then, the following message will be displayed:

------------------------------------
Topmost exception:
Connection error.

Inner exception:
Bad SQL statement.
------------------------------------

All in all, see the InnerException property of the Exception class:

http://msdn.microsoft.com/library/de...ptiontopic.asp

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/
Sep 7 '06 #3
Hi,

How can I send the Exception message back to the method (in the
Interface class) that called connectToDB()? In other words, how can I
display the error in the catch{} in my webform?
You are, or more precise you are providing the tools for the UI to know
what/where happened, (you are including the original throw exception :
catch(Exception ex)
{
throw new Exception("User::Login::Error occured.", ex);
}

Now in the webform you should handle this exception.

IIRC the framework will show this exception in the page (both message &
stacktrace )
Is there some type of log that automatically stores some of the error
information even if there's already code that handles it?
No,

For all
purposes, assume that I can't modify connectToDB() method because
everything's in the Production environment.
You can set a try/catch in the code that invoke it.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Sep 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Serge Calderara | last post by:
Dear all, I am buiding a quite huge application which is devided in quite many small functionnality that we will call modules here to simplify. I need to implement my exception handling...
11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
2
by: tom | last post by:
Hi, I am developing a WinForm application and I am looking for a guide on where to place Exception Handling. My application is designed into three tiers UI, Business Objects, and Data Access...
9
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in...
4
by: Ele | last post by:
When Exception handling disabled compiler still spits out "C++ exception handler used." Why is that? Why does it ask for "Specify /EHsc"? Thanks! c:\Program Files\Microsoft Visual Studio...
41
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
4
by: DHS1 | last post by:
Hey guys. I have a lab that is due in two weeks, but I wanted to start on it now. Problem is, I'm at home during christmas break so I can't ask my professors. Here's my problem: I am given a very...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.