Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Exceptions Could string = string throw besides System.OutOfMemoryException?

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

like image 210
AndrewJacksonZA Avatar asked Aug 26 '10 14:08

AndrewJacksonZA


People also ask

What is System OutOfMemoryException?

When data structures or data sets that reside in memory become so large that the common language runtime is unable to allocate enough contiguous memory for them, an OutOfMemoryException exception results.

How do I fix out of memory exception in C #?

To avoid this exception while working with StringBuilder, we can call the constructor StringBuilder. StringBuilder(Int32, Int32) and can set the MaxCapacity property to a value that will be large enough to serve the accommodation required when we expand the corresponding StringBuilder object.


2 Answers

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.

like image 173
Sergey Teplyakov Avatar answered Sep 27 '22 23:09

Sergey Teplyakov


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.

like image 30
Yves M. Avatar answered Sep 27 '22 22:09

Yves M.