|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Help with Exception handlingIn 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 Nietzsche VMI,
>I have two classes in my web app: the interface and the DB connection. If your code in connectToDB is throwing the new exception like this with two > 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()? 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/default.asp?url=/library/en-us/cpref/html/frlrfsystemexceptionclassinnerexceptiontopic.asp -- Regards, Mr. Jani Järvinen C# MVP Helsinki, Finland ja***@removethis.dystopia.fi http://www.saunalahti.fi/janij/ Hi,
> How can I send the Exception message back to the method (in the You are, or more precise you are providing the tools for the UI to know > Interface class) that called connectToDB()? In other words, how can I > display the error in the catch{} in my webform? 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 No,> information even if there's already code that handles it? For all > purposes, assume that I can't modify connectToDB() method because You can set a try/catch in the code that invoke it.> everything's in the Production environment. -- -- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation
how to prohibit writing to a collection by other threads ?
Queue.Contains(myobj) return always false.. Delegate Question: How does it know? Selecting a PictureBox Properties Get and Set creating 2 indexers in dotnet 2? Finding Current/Active Session ID Enum Serialization as Integer or Literal .NET/SQL viewer Windows Service on Win2003 server |
|||||||||||||||||||||||