您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 宿州分类信息网,免费分类信息发布

C#中如何捕获内存不足异常?

2024/4/14 2:17:47发布17次查看
当 clr 无法分配所需的足够内存时,会发生 system.outofmemoryexception。
system.outofmemoryexception 继承自 system.systemexception 类。
设置字符串 -
string studentname = "tom";string studentsubject = "maths";
现在您需要使用分配的容量进行初始化,即初始值的长度 -
stringbuilder sbuilder = new stringbuilder(studentname.length, studentname.length);
现在,如果您尝试插入附加值,则会发生异常。
sbuilder.insert(value: studentsubject, index: studentname.length - 1, count: 1);
出现以下异常 -
system.outofmemoryexception: out of memory
要捕获错误,请尝试以下代码 -
示例 实时演示
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); } } }}
上面处理 outofmemoryexception 并生成以下错误 -
输出error:system.outofmemoryexception: out of memory
以上就是c#中如何捕获内存不足异常?的详细内容。
宿州分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录