-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Document System.Drawing OutOfMemoryException to ExternalException breaking change for .NET 10 with C# and VB examples #47939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
970d5ed
Initial plan
Copilot bbdd2a8
Add System.Drawing OutOfMemoryException breaking change documentation…
Copilot ca13bcf
Move System.Drawing breaking change from core-libraries to windows-forms
Copilot 24cf26f
Add VB.NET example and language metadata for System.Drawing breaking …
Copilot 1a6989c
Remove tab structure from code examples, let metadata handle rendering
Copilot 4fbf2ed
Update docs/core/compatibility/windows-forms/10.0/system-drawing-outo…
adegeo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...ompatibility/windows-forms/10.0/system-drawing-outofmemory-externalexception.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| --- | ||
| title: "Breaking change: System.Drawing OutOfMemoryException changed to ExternalException" | ||
| description: "Learn about the breaking change in .NET 10 where System.Drawing GDI+ OutOfMemory errors now throw ExternalException instead of OutOfMemoryException." | ||
| ms.date: 08/15/2025 | ||
| ai-usage: ai-assisted | ||
| dev_langs: | ||
| - CSharp | ||
| - VB | ||
| --- | ||
|
|
||
| # System.Drawing OutOfMemoryException changed to ExternalException | ||
|
|
||
| GDI+ error handling in System.Drawing has been updated to throw <xref:System.Runtime.InteropServices.ExternalException> instead of <xref:System.OutOfMemoryException> for `Status.OutOfMemory` errors. | ||
|
|
||
| ## Version introduced | ||
|
|
||
| .NET 10 Preview 5 | ||
|
|
||
| ## Previous behavior | ||
|
|
||
| When GDI+ encountered `Status.OutOfMemory` errors (often due to invalid input rather than actual memory issues), System.Drawing APIs threw <xref:System.OutOfMemoryException>. | ||
|
|
||
| ## New behavior | ||
|
|
||
| When GDI+ encounters `Status.OutOfMemory` errors, System.Drawing APIs now throw <xref:System.Runtime.InteropServices.ExternalException> instead. | ||
adegeo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Type of breaking change | ||
|
|
||
| This is a [behavioral change](../../categories.md#behavioral-change). | ||
|
|
||
| ## Reason for change | ||
|
|
||
| GDI+ isn't particularly good at returning errors when it's unable to create internal objects. There are many cases where object creation fails due to invalid input, and higher-level code gets a null and turns it into `Status.OutOfMemory`. This is frequently a source of confusion since the error is often not related to actual memory issues. | ||
|
|
||
| The change to `ExternalException` provides more accurate error reporting, as this exception type is already thrown in other System.Drawing code paths for similar GDI+ errors. | ||
|
|
||
| ## Recommended action | ||
|
|
||
| If your code catches `OutOfMemoryException` when using System.Drawing APIs, ensure you also catch `ExternalException` to handle these GDI+ errors. | ||
|
|
||
| ```csharp | ||
| try | ||
| { | ||
| // System.Drawing operations | ||
| } | ||
| catch (ExternalException ex) | ||
| { | ||
| // Handle GDI+ errors (including former OutOfMemoryException cases) | ||
| } | ||
| catch (OutOfMemoryException ex) | ||
| { | ||
| // Handle actual memory issues | ||
| } | ||
| ``` | ||
|
|
||
| ```vb | ||
| Try | ||
| ' System.Drawing operations | ||
| Catch ex As ExternalException | ||
| ' Handle GDI+ errors (including former OutOfMemoryException cases) | ||
| Catch ex As OutOfMemoryException | ||
| ' Handle actual memory issues | ||
| End Try | ||
| ``` | ||
|
|
||
| ## Affected APIs | ||
|
|
||
| All System.Drawing APIs that interact with GDI+ and previously could throw `OutOfMemoryException` for `Status.OutOfMemory` errors, including but not limited to: | ||
|
|
||
| - <xref:System.Drawing.Bitmap?displayProperty=fullName> constructors and methods | ||
| - <xref:System.Drawing.Graphics?displayProperty=fullName> methods | ||
| - <xref:System.Drawing.Image?displayProperty=fullName> methods | ||
| - <xref:System.Drawing.Icon?displayProperty=fullName> constructors and methods | ||
| - Other System.Drawing types that use GDI+ internally | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.