1

What I want to do is to get out a connectionstring through configration manager.

public Database( string connectionName ) {
    try {
        connectionString = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
    } catch (ArgumentException exeption) {
        throw new ArgumentException("No connection name like this", connectionName, exeption);
    }
}

and I am getting:

Object reference not set to an instance of an object.

Yes, of course, because there is no connection string like that, I misspelled.

This is why I've tried to wrap it with a try / catch.

But I've got a Server error, not the exception.

How can I handle, if there is no connection string in the configuration file like connectionName.

vaso123
  • 12,347
  • 4
  • 34
  • 64
  • You're filtering your exceptions and when I generate an exception I get an NullReferenceException, not an ArgumentException. There simply is no handler for the exception that is thrown, which explains why the exception bubbles up the stack until an handler will catch it. – rene Jun 08 '17 at 11:41
  • Just check `ConfigurationManager.ConnectionStrings[connectionName]` for null, no need to generate and catch any exceptions. – Evk Jun 08 '17 at 11:59
  • @Evk Cool, thx, great, it works. If you add it as an answer, I will accept at. – vaso123 Jun 08 '17 at 12:38
  • Well I cannot do that because question is (correctly) marked as duplicate, but it's not a problem. – Evk Jun 08 '17 at 12:41

0 Answers0