import java.util.InputMismatchException ;  //数据类型错误
import java.lang.ArrayIndexOutOfBoundsException ; //越界
import java.util.Scanner ;public class boat
{
 public int i2 = 2;
          public int j2 = 2;
 
 public void boat()             
  {
   try
    { 
  Scanner sc = new Scanner(System.in);
         System.out.print("请输入战舰坐标(格式:(a b)):"); 
                  i2 = sc.nextInt();
         j2 = sc.nextInt();
    
   catch(InputMismatchException e)
    {
     System.out.println("数据错误!!!");
    }
   catch(ArrayIndexOutOfBoundsException e)
    {
     System.out.println("数据超出范围!!!");
    }
  }
}
当发生异常时,如何使程序返回到try前边(也就是boat方法刚开始)???

解决方案 »

  1.   

    用面向对象的语言做着面向过程的事,你想要goto啊
      

  2.   

    在System.out.println之后直接调用boat()方法不行么?
      

  3.   

    我试了试,可以的。
    但是在InputMismatchException所在的catch块中可以实现,
    在ArrayIndexOutOfBoundsException所在的catch块中就无法实现。
    提示Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
            at sea.seaprint(sea.java:16)
            at run.main(run.java:32)
    这是怎么回事啊??
      

  4.   

    不知道你怎么触发的下标越界,我触发不了。还有一个简单的办法,你写一个while循环,包裹你的语句。在while循环中,根据布尔值决定是否退出。然后通过输入,控制这个布尔值就可以达到你的效果了。
      

  5.   

    我是自己输入越界的坐标,但是就会出现
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
      at sea.seaprint(sea.java:16)
      at run.main(run.java:32)这是那个run类:
    public class run //定义运行类
    {
    public static void main(String args[])
    {
      submarine obj1 = new submarine(); 
               boat obj2 = new boat();              System.out.println("显示海域:");
     sea.seaprint(0,0,'S',2,2,'B');     
     while(true)
     {
       System.out.println("现在潜艇移动:");              obj1.submarine();
     if(search.find(obj1.i1, obj1.j1,obj2.i2, obj2.j2))    {
       System.out.println("潜艇发现战舰!!!");
      sea.seaprint(obj1.i1, obj1.j1, 'S', obj1.i1, obj1.j1, 'S'); 
     System.exit(1); }

          sea.seaprint(obj1.i1, obj1.j1, 'S', obj2.i2, obj2.j2, 'B');      
     
     
    System.out.println("现在战舰移动:");      obj2.boat();
    if(search.find(obj1.i1, obj1.j1,obj2.i2, obj2.j2))   {
      System.out.println("战舰发现战舰!!!");
    sea.seaprint(obj2.i2, obj2.j2, 'B', obj2.i2, obj2.j2, 'B');  System.exit(1); }
     sea.seaprint(obj1.i1, obj1.j1, 'S', obj2.i2, obj2.j2, 'B');   }
      }  
    }
    麻烦帮我看看是怎么回事啊??我急求答案
      

  6.   

    while(true){
    try
    {  
    Scanner sc = new Scanner(System.in);
    System.out.print("请输入战舰坐标(格式:(a b)):");  
      i2 = sc.nextInt();
    j2 = sc.nextInt(); 
    break;
    catch(InputMismatchException e)
    {
    System.out.println("数据错误!!!");
    }
    catch(ArrayIndexOutOfBoundsException e)
    {
    System.out.println("数据超出范围!!!");
    }}
      

  7.   

    简单点,只能用递归了。
    不过,我就奇怪了。为啥要有这种设计逻辑呢?
    很纠结。import java.util.InputMismatchException ; //数据类型错误
    import java.lang.ArrayIndexOutOfBoundsException ; //越界
    import java.util.Scanner ;public class boat
    {
      public int i2 = 2; 
      public int j2 = 2;
      
      public void boat()   
      {
        try
        {  
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入战舰坐标(格式:(a b)):");  
        i2 = sc.nextInt();
        j2 = sc.nextInt(); 
      
        catch(InputMismatchException e)
        {
          System.out.println("数据错误!!!");
          boat();
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
          System.out.println("数据超出范围!!!");
          boat();
        }
      }
    }
      

  8.   

    while (true)
    {
        try
        {
            正常代码(要返回的地方);
            if (条件正确)
            {
                break;
            }
        }
        catch (Exception e)
        {
           e.printStackTrace();
        }
    }