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/