Help with Exception handling

  • Thread starter Thread starter VMI
  • Start date Start date
V

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.
 
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

V> I have two classes in my web app: the interface and the DB
V> connection.
V> In the method that connects to the DB, there's a try..catch statement
V> that looks like this:
V> public DataTable connectToDB()
V> {
V> try
V> {
V> // CONNECT ERROR. Wrong server name
V> }
V> catch(Exception ex)
V> {
V> throw new Exception("User::Login::Error occured.", ex);
V> }
V> How can I send the Exception message back to the method (in the
V> Interface class) that called connectToDB()? In other words, how can I
V> display the error in the catch{} in my webform?
V> Is there some type of log that automatically stores some of the error
V> information even if there's already code that handles it? For all
V> purposes, assume that I can't modify connectToDB() method because
V> everything's in the Production environment.
V> Thanks.
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 Nietzsch
 
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/d...rfsystemexceptionclassinnerexceptiontopic.asp

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top