This question already has answers here:
How to avoid ArrayIndexOutOfBoundsException or IndexOutOfBoundsException? [duplicate]
                                
                                    (2个答案)
                                
                        
                                3年前关闭。
            
                    
大家好,这是我的第一个问题
如果没有选择任何行,我想抓住它并显示一条消息。

private void table_Order_EKeyPressed(java.awt.event.KeyEvent evt) {
          int row = table_Order_E.getSelectedRow();

             if (evt.getKeyCode() == KeyEvent.VK_INSERT)
             {

             }

             try{

             if ( evt.getKeyCode()==KeyEvent.VK_DELETE && row<0 )
             {

                 System.err.println("No Row has been selected..."+row);

             }else if(evt.getKeyCode()==KeyEvent.VK_DELETE && row >-1)
             {
                 model.removeRow(row);//remov with delete key.

             }
             }catch(ArrayIndexOutOfBoundsException e){
                 JOptionPane.showMessageDialog(null, e);
             }
        }

最佳答案

您必须添加一个if条件,并从try块中引发异常。
请参考http://beginnersbook.com/2013/04/throw-in-java/以获取更多参考

07-26 07:55