P
Patrick
Hi,
It seems that "throw new Expcetion(...)" (in C#) is not a good
practice because the exception is too generic to catch without ignoring
some low level exceptions such as OutOfMemoryException and
StackOverflowException. For me, I cannot figure out any advantage to
throw Exception directly except forcing the client to use "catch
(Exception ex)" or to prevent this exception from throwing by checking
some conditions.
For example, System.Drawing.Graphics.FromImage(Image image) will
throw Exception if the specified image is an image of indexed pixels. I
know I can check (image.PixelFormat & PixelFormat.Indexed) before
calling FromImage() to prevent this problem. However, in other cases,
there may not be any condition I can check in advance before the
Exception is thrown.
Hence, here are my questions: Is "throw new Exception(...)" a good
practice? If not, why do Microsoft programmers implement it in this
way?
Patrick
It seems that "throw new Expcetion(...)" (in C#) is not a good
practice because the exception is too generic to catch without ignoring
some low level exceptions such as OutOfMemoryException and
StackOverflowException. For me, I cannot figure out any advantage to
throw Exception directly except forcing the client to use "catch
(Exception ex)" or to prevent this exception from throwing by checking
some conditions.
For example, System.Drawing.Graphics.FromImage(Image image) will
throw Exception if the specified image is an image of indexed pixels. I
know I can check (image.PixelFormat & PixelFormat.Indexed) before
calling FromImage() to prevent this problem. However, in other cases,
there may not be any condition I can check in advance before the
Exception is thrown.
Hence, here are my questions: Is "throw new Exception(...)" a good
practice? If not, why do Microsoft programmers implement it in this
way?
Patrick