程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了IndexOutOfRangeException (C#) 与 ArrayIndexOutOfBoundsException(Java)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决IndexOutOfRangeException (C#) 与 ArrayIndexOutOfBoundsException(Java)?

开发过程中遇到IndexOutOfRangeException (C#) 与 ArrayIndexOutOfBoundsException(Java)的问题如何解决?下面主要结合日常开发的经验,给出你关于IndexOutOfRangeException (C#) 与 ArrayIndexOutOfBoundsException(Java)的解决方法建议,希望对你解决IndexOutOfRangeException (C#) 与 ArrayIndexOutOfBoundsException(Java)有所启发或帮助;

我只是在为我的一个朋友实现一个很酷且简单的棋盘(棋盘风格)游戏。 由于他希望游戏在 androID 和 windows 桌面上运行,因此我正在使用 java 在 AndroID studio 中实现 androID 版本,在 VS2019 C# .NET 中实现 windows 桌面版本。

我现在正在尝试将以下 java 异常移植到 C# .NET;这是java代码:

package eu.georgtoth.game.exceptions;

import eu.georgtoth.game.constants.bOARDcolumN;
import java.lang.Arrayindexoutofboundsexception;
import java.lang.indexoutofboundsexception;
/**
 * Boardindexoutofboundsexception extends {@link Arrayindexoutofboundsexception}
 * thrown,when column {@link BOARDcolumN} and row {@link Integer} are out of board bounces!
 */
public class Boardindexoutofboundsexception extends Arrayindexoutofboundsexception {
    /**
     * constructor with message calling super ctor {@link Arrayindexoutofboundsexception(String s)}
     * @param message {@link String} for exception message
     */
    public Boardindexoutofboundsexception(String messagE) { super(messagE); }

    /**
     * constructor with message and inner exception using {@link #initCause(Throwable throwablE)}
     * @param message {@link String} detailed exception message
     * @param throwable {@link ThrowablE} for inner exception 
     */
    public Boardindexoutofboundsexception(String message,Throwable throwablE) {
        this(messagE);
        super.initCause(throwablE);
    }

    /**
     * constructor with board column and row indexer,calling base ctor 
     *   with inner exception (@link indexoutofboundsexception) as Throwable {@link ThrowablE}
     * @param column board {@link BOARDcolumN} column indexer
     * @param row    board {@link Integer} row indexer
     */
    public Boardindexoutofboundsexception(BOARDcolumN column,int row) {
        this("board accessor out of bounce at fIEld: " + column.getname() + row,((indexoutofboundsexception) 
                (new Throwable("out of bounds at colunn=" + column.getValue() + ",row=" + row))));
    }
}

https://docs.Oracle.com/javase/9/docs/api/java/lang/ArrayIndexOutOfBoundsException.html https://docs.Oracle.com/javase/9/docs/api/java/lang/IndexOutOfBoundsException.html

我有以下问题/疑问:

  1. C#中没有具体的ArrayIndexOutOfSomewhatException,只有IndexOutOfRangeException。

  2. IndexOutOfRangeException 是一个密封的(某种框架)类,不能被继承。

  3. 但是 .NET 中还有另一个“出于某种原因”的异常,称为 ArgumentOutOfRangeException

我仍然没有完全从语义上完全理解 .NET 异常的某种更平坦的层次结构,在 Exception 子类树中具有许多相似的兄弟(叶节点)。 (使用 C# .NET 8 年后的悲剧)

谁能解释一下,什么时候应该使用 IndexOutOfRangeException,什么时候 ArgumentOutOfRangeException 在语义上是正确的?

在这种情况下,语义上正确的选择是什么?

  • 在没有派生类的情况下调用简单的 IndexOutOfRangeException。 (看起来不像时髦的代码)?
  • 调用简单的 ArgumentOutOfRangeException?
  • 实现从 ArgumentOutOfRangeException 派生的框架子类?

亲切的问候,海因里希。

ps:我知道这个问题是一个迂腐的怪癖,但我真的不明白这里的确切形式差异。 (通常在时间压力下的工作中,我会在与同事协商(投票或老板决定)后实施一些变体,但与 Java 相比,.NET 的异常层次结构对我来说并不是完全一致的

解决方法

IndexOutOfRangeException 用于当您尝试访问索引超出范围的数组或集合的元素时。例如,访问大小为 10 的数组的第 11 个元素。

ArgumentOutOfRangeException 不涉及数组。当值超出范围时使用它。例如,将某人的生日设置为第 13 个月。

你应该扩展哪个? Board index out of bounds 听起来有点像有人试图用超出范围的索引定位某些东西。似乎 IndexOutOfRangeException 是一个合理的选择。但是,它扩展了 SystemException,这可能不适合您的应用程序的异常。

有什么理由不只是扩展 Exception 吗?微软的文档似乎暗示了这一点: https://docs.microsoft.com/en-us/dotnet/standard/exceptions/how-to-create-user-defined-exceptions

他们的最佳实践文档建议尽可能使用内置异常。 IndexOutOfRangeException 不适合你吗? https://docs.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions

大佬总结

以上是大佬教程为你收集整理的IndexOutOfRangeException (C#) 与 ArrayIndexOutOfBoundsException(Java)全部内容,希望文章能够帮你解决IndexOutOfRangeException (C#) 与 ArrayIndexOutOfBoundsException(Java)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。