English 中文(简体)
如果允许在阵列中添加明显的例外类型?
原标题:Correct exception type for adding to an array when it isn t allowed?

请允许我说,我有一个执行<代码>ArrayAccess的类别,但不允许增加新的数据。 你可以确定已经存在的价值观,但你可以增加规模。

$arrayAccessible[1] = new StdClass(); //okay
$arrayAccessible[] = new StdClass(); //should throw an exception

What exception type should I throw? I do have SPL exception types available and should use them if it makes sense. The best I can think of is perhaps BadFunctionCallException or BadMethodCallException.

如果说不错的话,也许我会打上<条码>,不支持行动,。 然而,这肯定不是我喜欢做的事。 在这个项目中,很难增加这一点。

最佳回答

Honestly, I would throw an OverflowException. Since the array is fixed, it is by definition full as well...

问题回答

I d say UnsupportedOperationException is good enough - it describes the problem that occurred well and fellow developer should be able to figure out what happened easily. If you aren t sure or want to be more specific, create your own exception.

Also, I wouldn t probably use ArrayAccess (unless you are working with legacy code) if I wanted to have fields read only. Getters should do the trick. If you want to iterate over object s properties, implement also Iterator interface.





相关问题
handling exceptions IN Action Filters

Is there a better way to handle exceptions that occur inside an Action Filter itself in ASP .NET MVC? There re 2 ways I can think of at the moment. Using a try catch and setting the HTTP Status ...

既可捕获,又可举出例外。

我有一种办法,可以进入亚洲开发银行,因此,我国的亚行在多瑙河航道中的所有 st子都位于一个试捕区。 它正在追捕Kexception

Cross compiler exception handling - Can it be done safely?

I am doing some maintenance on a C++ windows dll library that is required to work with different VC++ compilers (as I don’t want to address different mangling schemes). I have already eliminated any ...

File Handling Issue

I am developing a tool in c#, at one instance I start writing into a xml file continuously using my tool,when i suddenly restart my machine the particular xml file gets corrupted, what is the reason ...

Watch a memory location/install data breakpoint from code?

We have a memory overwrite problem. At some point, during the course of our program, a memory location is being overwritten and causing our program to crash. the problem happens only in release mode. ...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签