积极答复者
请教大家,关于ArgumentException异常!

问题
-
请教大家!我的测试代码如下:
Graphics g = this.CreateGraphics();
using (g)
{
g.FillRectangle(Brushes.Red, 0, 0, 100, 100);
}
g.FillRectangle(Brushes.Red, 0, 0, 100, 100);//这里g占用的资源已经释放,但是这里抛出的是ArgumentException异常,我的疑问是为什么会抛出这个类型的异常,实际上这里的参数没有什么问题啊!
周雪峰- 已移动 Sheng Jiang 蒋晟Moderator 2009年3月13日 19:25 .Net基础类库问题 (从 Visual C# 移动到 .NET Framework 一般性问题讨论区)
答案
-
因为它实际调用的是
[DllImport("gdiplus.dll", CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
internal static extern int GdipFillRectangleI(HandleRef graphics, HandleRef brush, int x, int y, int width, int height);
graphics是它的参数
http://feiyun0112.cnblogs.com/- 已标记为答案 周雪峰MVP, Moderator 2009年3月10日 7:40
-
以下是FillRectangle方法反射后得到的源码。
1 if (brush == null) 2 { 3 throw new ArgumentNullException("brush"); 4 } 5 int status = SafeNativeMethods.Gdip.GdipFillRectangle(new HandleRef(this, this.NativeGraphics), new HandleRef(brush, brush.NativeBrush), x, y, width, height); 6 this.CheckErrorStatus(status); 7 8
然后得到的结果是status整数值,然后CheckErrorStatus方法反射得到的源代码中throw StatusException,StatusException中,构造函数是这样的:1 internal static Exception StatusException(int status) 2 { 3 switch (status) 4 { 5 //1至11省略 6 7 case 12: 8 return new ExternalException(SR.GetString("GdiplusAccessDenied"), -2147024891); 9 10 case 13: 11 return new ArgumentException(SR.GetString("GdiplusUnknownImageFormat")); 12 //14之后的省略 13 } 14 } 所以这个异常是根据返回的status值转换成对应的Exception
理解的越多,需要记忆的就越少- 已标记为答案 周雪峰MVP, Moderator 2009年3月10日 7:40
全部回复
-
因为它实际调用的是
[DllImport("gdiplus.dll", CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
internal static extern int GdipFillRectangleI(HandleRef graphics, HandleRef brush, int x, int y, int width, int height);
graphics是它的参数
http://feiyun0112.cnblogs.com/- 已标记为答案 周雪峰MVP, Moderator 2009年3月10日 7:40
-
以下是FillRectangle方法反射后得到的源码。
1 if (brush == null) 2 { 3 throw new ArgumentNullException("brush"); 4 } 5 int status = SafeNativeMethods.Gdip.GdipFillRectangle(new HandleRef(this, this.NativeGraphics), new HandleRef(brush, brush.NativeBrush), x, y, width, height); 6 this.CheckErrorStatus(status); 7 8
然后得到的结果是status整数值,然后CheckErrorStatus方法反射得到的源代码中throw StatusException,StatusException中,构造函数是这样的:1 internal static Exception StatusException(int status) 2 { 3 switch (status) 4 { 5 //1至11省略 6 7 case 12: 8 return new ExternalException(SR.GetString("GdiplusAccessDenied"), -2147024891); 9 10 case 13: 11 return new ArgumentException(SR.GetString("GdiplusUnknownImageFormat")); 12 //14之后的省略 13 } 14 } 所以这个异常是根据返回的status值转换成对应的Exception
理解的越多,需要记忆的就越少- 已标记为答案 周雪峰MVP, Moderator 2009年3月10日 7:40