locked
How do I make Exceptions "bubble" up? RRS feed

  • Question

  • User1815952226 posted
    I am not able to make an exception "bubble" to the calling function and not any further. I have these two functions: public static string getConnectionString() { if(connectionString != null) return connectionString; else { log.Fatal("In getConnectionString() - Could not read the db connection string from the Web.config file"); throw new Exception("In getConnectionString() - Could not read the db connection string from the Web.config file"); } } public static SqlConnection getSqlConnection() { try { return new SqlConnection(getConnectionString()); } catch(Exception ex) { throw new Exception("In getSqlConnection(), ex"); } } If the first function throws an exception, the second function catches it but it is not thrown to the function calling the second function even though I am calling the second function within a try/catch. When the exception is caught by the second function, the customerrors page is displayed and the app halts. I want the exception to bubble all the way up to the interface code - is this possible or is there a limit as to how many times you can throw the same exception? Many thanks, Stian Danielsen
    Monday, February 16, 2004 4:28 AM

All replies

  • User1815952226 posted
    A small correction in my code example. In the second function: throw new Exception("In getSqlConnection(), ex"); should be: throw new Exception("In getSqlConnection()", ex); Stian
    Monday, February 16, 2004 5:46 AM