
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to capture out of memory exception in C#?
The System.OutOfMemoryException occurs when the CLR fail in allocating enough memory that is needed.
System.OutOfMemoryException is inherited from the System.SystemException class.
Set the strings −
string StudentName = "Tom"; string StudentSubject = "Maths";
Now you need to initialize with allocated Capacity that is the length of initial value −
StringBuilder sBuilder = new StringBuilder(StudentName.Length, StudentName.Length);
Now, if you will try to insert additional value, the exception occurs.
sBuilder.Insert(value: StudentSubject, index: StudentName.Length - 1, count: 1);
The following exception occurs −
System.OutOfMemoryException: Out of memory
To capture the error, try the following code −
Example
using System; using System.Text; namespace Demo { class Program { static void Main(string[] args) { try { string StudentName = "Tom"; string StudentSubject = "Maths"; StringBuilder sBuilder = new StringBuilder(StudentName.Length, StudentName.Length); // Append initial value sBuilder.Append(StudentName); sBuilder.Insert(value: StudentSubject, index: StudentName.Length - 1, count: 1); } catch (System.OutOfMemoryException e) { Console.WriteLine("Error:"); Console.WriteLine(e); } } } }
The above handles OutOfMemoryException and generates the following error −
Output
Error: System.OutOfMemoryException: Out of memory
- Related Articles
- How to capture index out of range exception in C#?
- How to capture out of array index out of bounds exception in Java?
- How to solve “Process out of Memory Exception” in Node.js?
- Out of memory exception in Java:\n
- How to capture null reference exception in C#?
- How to capture divide by zero exception in C#?
- How to capture file not found exception in C#?
- How to capture divide by zero exception in Java?
- How to capture file not found exception in Java?
- How to capture and print Python exception message?
- How to capture an exception raised by a Python Regular expression?
- How to handle Java Array Index Out of Bounds Exception?
- How to solve JavaScript heap out of memory on prime number?
- How to capture screenshots in Selenium?
- How to capture SIGINT in Python?

Advertisements