c# – 如何抛出下溢异常?
发布时间:2020-12-15 23:37:08 所属栏目:百科 来源:网络整理
导读:在C#中我可以抛出溢出异常: throw new System.OverflowException("Cannot push onto a full stack."); 如何抛出下溢异常? throw new System.UnderflowException("Cannot pop from an empty stack."); 它看起来不像UnderflowException是System的一种方法.
在C#中我可以抛出溢出异常:
throw new System.OverflowException("Cannot push onto a full stack."); 如何抛出下溢异常? throw new System.UnderflowException("Cannot pop from an empty stack."); 它看起来不像UnderflowException是System的一种方法. 解决方法
没有UnderflowException.如果你这样做:
var stack = new Stack(); stack.Push(1); var x1 = stack.Pop(); var x2 = stack.Pop(); 您将获得InvalidOperationException:
但是你可以完全自由地创建自己的Exception类: public class UnderflowException : Exception { public UnderflowException(string message): base(message) { } } 如果你需要,扔掉它: throw new UnderflowException("Could not pop from empty stack"); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |