开发者

how does trycatch hurt the memory/CPU?

开发者 https://www.devze.com 2023-04-11 21:10 出处:网络
so I am interested in the c# side of it - but I am tagging c++ as the concept exists there and I am not over the \'finally\' keyword.

so I am interested in the c# side of it - but I am tagging c++ as the concept exists there and I am not over the 'finally' keyword. So anyway - are there any benchmarks online about how try-catch would slow down or will use more memory than simple 'if-else' or other code?? For instance, now I am writing a code and using Streamwriter which shows 7 possible exceptions when you hold your mouse over it...so would anyone claim that it will be faster if i write something like:

//////////////
if(path is not too long)
{ if(file exists)
{ if(nothing else uses the file)
{ if(user is authorized)
}}}
////////////

You have 7 conditions and you can use instead just try-catch - not to mention that these conditions can开发者_运维问答not be simplified to a single if statement.

10x!


try/catch has a slight performance cost if the exception occurs, but compared to file access the cost is not worth worrying about.

More important is that try/catch is correct, the nested ifs are not, because the filesystem is a shared resource that can be modified asynchronously. Handling the error or exception result of actually opening the file is the only way to avoid a race condition.

See

  • http://blogs.msdn.com/b/jaredpar/archive/2009/12/10/the-file-system-is-unpredictable.aspx
  • http://blogs.msdn.com/b/ericlippert/archive/2008/09/10/vexing-exceptions.aspx
  • how can you easily check if access is denied for a file in .NET?

These examples all use files, but there's a general principle here for shared resources.


Any performance differences are not really the point.

Testing that a file exists at some point before opening it does not guarantee that another thread or process has not deleted or locked the file; you only know at the point of attempting to open it whether or not you have succeeded. Hence OS calls often return success or failure codes, or return a known value to indicate an invalid file handle.

In an object oriented environment, it's cleaner to use the idiom that creating the object acquires the resource (RAII) then you have to throw an exception rather than allow an invalid object to be created. Alternatively, you could have an object-oriented OS interface which returned a reference to a known bad object ( e.g. null ), but then you're delaying the exception to a later point in the program, so may not know what file failed to open.

The other advantage is that it allows the library providing the object to decide what the conditions for its validity are - if the file were actually a link to a WebDav server say, then other conditions such as presence of a valid network interface would be required. By having the library take care of the conditions, the implementation details are hidden from the client code (this applies to library APIs as to try/catch)


For a truly brilliant discourse on Exceptions handling and its ramifications (be it cost, system stability, etc..) I recommend reading Chapter 20 of the great work "CLR via C#" by Jeffrey Richter. It will definitely answer most of your questions and help understand how .NET framework handles exceptions under the hood, and how use Performance Counter as a mean to assess the impact of the exceptions your code throws.

Using Guard Clauses (as you mention in your question) is not an alternative to exception handling, they are two complimentary concepts. A guard condition such as if(file exists) might short circuit the rest of the conditions and yield better performance since your code may not need to test the following conditions i.e. if(user is authorized) might be resource intensive or time consuming.

0

精彩评论

暂无评论...
验证码 换一张
取 消