I am writing a class that performs operations on multiple threads. Here is an example of what I'm doing now
Dictionary<int, int> dict = new Dictionary<int, int>(_Streams.Count); for (int i = 0; i < _Streams.Count; i++) { try { dict.Add(i, _Streams[i].Read(buffer, offset, count)); } catch (System.IO.IOException e) { throw new System.IO.IOException(String.Format("I/O exception occurred in stream {0}", i), e); } catch (System.NotSupportedException e) { throw new System.NotSupportedException(String.Format("The reading of the stream {0} is not supported", i), e); } catch (System.ObjectDisposedException e) { throw new System.ObjectDisposedException(String.Format("Stream {0} is Disposed", i), e); } } int? last = null; foreach (var i in dict) { if (last == null) last = i.Value; if (last != i.Value) throw new ReadStreamsDiffrentExecption(dict); last = i.Value; } return (int)last;
I would like to simplify my code to
Dictionary<int, int> dict = new Dictionary<int, int>(_Streams.Count); for (int i = 0; i < _Streams.Count; i++) { try { dict.Add(i, _Streams[i].Read(buffer, offset, count)); } catch (Exception e) { throw new Exception(String.Format("Exception occurred in stream {0}", i), e); } } int? last = null; foreach (var i in dict) { if (last == null) last = i.Value; if (last != i.Value) throw new ReadStreamsDiffrentExecption(dict); last = i.Value; } return (int)last;
However, if someone tries to catch certain exceptions, my shell will hide the exception that Read will read. How to save the type of exception, add additional information, but you do not need to write a handler for every possible unexpectedness in the try block.
source share