views:

132

answers:

8

The code:

  public Constructor(string vConnection_String)
  {
     try
     {
        mConnection_String = vConnection_String;
     }
     catch (Exception ex)
     {
        ExceptionHandler.CatchEx(ex);
     }
  }

I think the person who programmed this was "just being careful," but out of interest what exceptions could be thrown on a line that does a string assignment like this one here? I can think of System.OutOfMemoryException, but what others?

Thank you

+1  A: 

An OutOfMemoryException is not very likely to happen here, because the string is not copied and no new memory needs to be allocated.

0xA3
+2  A: 

None that I can think of. Not even an out-of-memory exception. Strings are stored in a string pool. If you have the same string twice in your program, they both reference the same string instance in the string pool.

See also the documentation for String.Intern().

EDIT: As pointed out in a comment, the string pool is irrelevant here since this is simply a reference assignment (but some info about it is useful nonetheless, although it has nothing to do with the question, sorry about that). Both variables will point to the exact same object in memory after the assignment and no new memory is claimed.

Ronald Wildenberg
No, this is only true for string literals, i.e. constant string expressions in your code or strings that have explicitly interned. `string s1 = "Hello";` and `string s2 = "Hell" + 'o';` are actually different string objects and `object.ReferenceEquals(s1, s2)` will return false although both strings are equal.
0xA3
James Curran
You're absolutely right. I'll update my answer... Thanks.
Ronald Wildenberg
+3  A: 

Nothing can happen here in my point of view. If you use something like subversion then you will probably see that someone removed some code here without removing the exception handling. Otherwise it is just silly.

You can remove the verbose code without any doubts.

Yves M.
+3  A: 

I can't see how that would generate any exeception at all. I imagine the programmer merely has a ctor tempate he uses:

 try 
 { 
    /// Put Ctor code here!
 } 
 catch (Exception ex) 
 { 
    ExceptionHandler.CatchEx(ex); 
 } 
James Curran
A: 

If mConnection_String has any modifiers on it such as const or static it would throw an exception regarding access.

Joel Etherton
`const` would not even compile, and assigning to a `static` field would be fine.
0xA3
+1  A: 

...ThreadAbortException? (But it will get thrown again after the catch block.)

Eric Mickelsen
Not necessarily: for all we know **ExceptionHandler** could reset thread aborts! (Yeah, it's a stretch.)
Paul Ruane
@Paul: Good point - that's a possibility as well.
Eric Mickelsen
A: 

The exceptions thrown would depend on what mConnection_String is.

If mConnection_String is a field, the it's very unlikely to thrown any Exceptions. The Try..Catch logic is probably just there as standard boiler plate code so that when code gets added later, it's in the try...catch block.

If mConnection_String is a property, then it can thrown any exception which can be thrown (and not caught) in the set property. You'd have to look in the set property to see what's possible.

Thracx
+2  A: 

Herb Sutter write several great articles about exception safety, and in one of them he shows 3 types of exception safety:

  1. the basic guarantee

  2. the strong guarantee

  3. the nothrow guarantee

This principles are commonly known in C++ world but we could use them in .net world too because one of them takes place in your situation.

If mConnection_String is a field of type System.String (or another reference type) than you definitely know, that this code is "nothrow guarantee", because simple assignment for reference type could not throw exceptions at all.

Sergey Teplyakov
Thanks for the reply and the informative link Sergey.
AndrewJacksonZA