0

インデックスが範囲外のときにエラー メッセージを表示しようとしていますが、配列リストに要素がない場合でも、現在のコードでは適切なエラー メッセージが表示されません。

これは、例外を実装しようとしているセクションのコードブロックです。説明すると、make call メソッドは、配列要素に対応する番号である表示を取得し、正しいインデックスからの呼び出しを表示します。

public void makeCall()
{
    Mobile phoneCall = (Mobile) gadgets.get(getDisplay());
    phoneCall.PhoneCall(getPhoneNumber(), getDuration());
    System.out.println();
}

public int getDisplay()
{
    int gadgetDisplay = 0;
    try
    {
        gadgetDisplay = Integer.parseInt(displayText.getText());

    if (gadgetDisplay< 0)
    {
      JOptionPane.showMessageDialog
      (frame, "Please enter a positive Display");  
    }
    }
    catch(NumberFormatException exception)
    {
        JOptionPane.showMessageDialog
        (frame, "Please enter a positive Display");
    }
    catch(IndexOutOfBoundsException exception)
    {
        JOptionPane.showMessageDialog
        (frame, "Gadget is not listed");
    }
    return gadgetDisplay;
}
4

4 に答える 4

0

これを行うために try / catch ブロックを使用しないでください。ArrayList に含まれる要素の数は既にわかっているため、size()を使用して、数値が範囲内にあることを確認してください。

于 2013-04-23T11:14:40.763 に答える
0

サイズを確認する必要がある場合は、size() メソッドを呼び出すことができます。また、例外をスローして、makeCall メソッドがアクションを決定できるようにすることもできます。

public void makeCall() {
    try {
        Mobile phoneCall = (Mobile) gadgets.get(getDisplay());
        phoneCall.PhoneCall(getPhoneNumber(), getDuration());
        System.out.println();
    }
    catch(NumberFormatException exception) {
        JOptionPane.showMessageDialog(frame, "Please enter a positive Display");
    }
    catch(IndexOutOfBoundsException exception) {
        JOptionPane.showMessageDialog(frame, "Gadget is not listed");
    }
    catch(IllegalArgumentException exception) {
        JOptionPane.showMessageDialog(frame, "Please enter a positive Display");
    }
}

public int getDisplay() throws NumberFormatException
{
    int gadgetDisplay = Integer.parseInt(displayText.getText());

    if (gadgetDisplay < 0) {
        throw new IllegalArgumentException("Please enter a positive Display");  
    }
    else if ( gadgetDisplay >= yourList.size() ) {
        throw new IndexOutOfBoundsException("Gadget is not listed");
    }

    return gadgetDisplay;
}

または、1 つの方法でそれを行うことができます。

public void makeCall() {
    try {
        int gadgetDisplay = Integer.parseInt(displayText.getText());

        if (gadgetDisplay < 0) {
            JOptionPane.showMessageDialog(frame, "Please enter a positive Display");
        }
        else if ( gadgetDisplay >= yourList.size() ) {
            JOptionPane.showMessageDialog(frame, "Gadget is not listed");
        }
        else {
            Mobile phoneCall = (Mobile) gadgets.get(getDisplay());
            phoneCall.PhoneCall(getPhoneNumber(), getDuration());
            System.out.println();
        }
    }
    catch(NumberFormatException exception) {
        JOptionPane.showMessageDialog(frame, "Please enter a positive Display");
    }
}
于 2013-04-23T11:21:46.103 に答える
0

2 番目の catch ブロックは、try ブロックのコードがIndexOutOfBoundsException.

現在、その例外をスローする try ブロックには何もありません。は他の場所で処理されると言うように、独自の try catch ブロック内ArrayListの要素にアクセスしようとしているコードを配置する必要がある可能性が最も高いでしょう。ArrayListそこで例外を処理する必要があります。

正直に言うと、ここに間違ったコードを投稿したと思います。

于 2013-04-23T10:56:31.500 に答える
0

何かをしようとしていないため、Desired 例外は生成されないため、例外が発生します。

OutOfBound 例外は、範囲外から値を取得しようとしていることを意味します

public int getDisplay()
{
    int gadgetDisplay = 0;
    ArrayList arr = new ArrayList();
    arr.add("a");
    try
    {
        String str = arr.get(3);

    if (gadgetDisplay< 0)
    {
      JOptionPane.showMessageDialog
      (frame, "Please enter a positive Display");  
    }
    }
    catch(NumberFormatException exception)
    {
        JOptionPane.showMessageDialog
        (frame, "Please enter a positive Display");
    }
    catch(IndexOutOfBoundsException exception)
    {
        JOptionPane.showMessageDialog
        (frame, "Gadget is not listed");
    }
    return gadgetDisplay;
}

上記のコードは例外を生成します。そして、ArrayListからの例外をどのように期待しているのかわかりません

于 2013-04-23T10:58:19.663 に答える