Java 为什么CodeChef';s编译器抱怨我的代码需要抛出异常?

Java 为什么CodeChef';s编译器抱怨我的代码需要抛出异常?,java,exception-handling,Java,Exception Handling,当我尝试在CodeChef的编译器中运行代码时,它说我需要抛出某种异常。但是,该代码在Eclipse和JRE 8u20中运行良好。这是为了解决目前的问题。有人能帮我吗 我收到的信息是: NZEC代表非零出口代码。对于C用户,如果您的main方法没有返回0,则会生成此消息;陈述如果Java/C++等其他语言抛出异常,则可能会生成此错误 这是我的密码: import java.util.Scanner; public class Snake { int N, M, x, y, L; String p

当我尝试在CodeChef的编译器中运行代码时,它说我需要抛出某种异常。但是,该代码在Eclipse和JRE 8u20中运行良好。这是为了解决目前的问题。有人能帮我吗

我收到的信息是:

NZEC代表非零出口代码。对于C用户,如果您的main方法没有返回0,则会生成此消息;陈述如果Java/C++等其他语言抛出异常,则可能会生成此错误

这是我的密码:

import java.util.Scanner;
public class Snake {
int N, M, x, y, L;
String path;
public Snake(int N, int M, int x, int y, int L,String path) {
    this.N=N;
    this.M=M;
    this.x=x;
    this.y=y;
    this.L=L;
    this.path=path;
}

public void snaky(){
    int[][] area =new int[M][N];
    for(int i=0;i<M;i++)
        for(int j1=0;j1<N;j1++)
            area[i][j1]=0;
    int j=2;
    Snake[] s = new Snake[L];
    for(int i=0;i<L;i++)
        s[i] = new Snake(0,0,0,0,0,path);
    s[L-1] = new Snake(0,0,x,y,0,path);
    area[x][y]=1;
    char[] moves = path.toCharArray();
    int k=0;
    while(j<=L){
        char turn=moves[k++];
        if(turn=='U'){
            s[L-j++] = new Snake(0,0,x-1,y,0,path);
            area[x-1][y]=1;
            x=x-1;
            continue;
        }
        if(turn=='D'){
            s[L-j++] = new Snake(0,0,x+1,y,0,path);
            area[x+1][y]=1;
            x=x+1;
            continue;
        }
        if(turn=='L'){
            s[L-j++] = new Snake(0,0,x,y-1,0,path);
            area[x][y-1]=1;
            y=y-1;
            continue;
        }
        if(turn=='R'){
            s[L-j++] = new Snake(0,0,x,y+1,0,path);
            area[x][y+1]=1;
            y=y+1;
            continue;
        }

    }   
    char last = moves[L-2];
    int count=1;
    if(last=='U'){
            while(x>0){
                    if(area[x-1][y]==0){
                        area[x-1][y]=1;
                        area[s[L-count].x][s[L-count].y]=0;
                        count++;
                        x=x-1;
                    }
                    else{
                        System.out.println("BODY"+" "+(count-1));
                        return;
                    }
            }
            System.out.println("WALL"+" "+(count-1));
    }
    if(last=='D'){
        while(x<M){
                if(area[x+1][y]==0){
                    area[x+1][y]=1;
                    area[s[L-count].x][s[L-count].y]=0;
                    count++;
                    x=x+1;
                }
                else{
                    System.out.println("BODY"+" "+(count-1));
                    return;
                }
        }
        System.out.println("WALL"+" "+(count-1));
    }
    if(last=='L'){
        while(y>0){
                if(area[x][y-1]==0){
                    area[x][y-1]=1;
                    area[s[L-count].x][s[L-count].y]=0;
                    count++;
                    y=y-1;
                }
                else{
                    System.out.println("BODY"+" "+(count-1));
                    return;
                }
        }
        System.out.println("WALL"+" "+(count-1));
    }
    if(last=='R'){
        while(y<N){
                if(area[x][y+1]==0){
                    area[x][y+1]=1;
                    area[s[L-count].x][s[L-count].y]=0;
                    count++;
                    y=y+1;
                }
                else{
                    System.out.println("BODY"+" "+(count-1));
                    return;
                }
        }
        System.out.println("WALL"+" "+(count-1));
    }   
}

public static void main(String[] args){
    Scanner reader = new Scanner(System.in);
    int T = reader.nextInt();
    Snake[] s = new Snake[T];
    for(int i=0;i<T;i++){
        int N = reader.nextInt();
        int M = reader.nextInt();
        int x = reader.nextInt();
        int y = reader.nextInt();
        int L = reader.nextInt();
        String path = reader.next();
        s[i] = new Snake(N,M,M-y,x-1,L,path);
    }
    for(int i=0;i<T;i++){
        s[i].snaky();
    }
    reader.close();
}

}
import java.util.Scanner;
公营蛇{
int N,M,x,y,L;
字符串路径;
公共蛇(int N,int M,int x,int y,int L,字符串路径){
这个,N=N;
这个,M=M;
这个.x=x;
这个。y=y;
这个。L=L;
this.path=path;
}
公共空间蛇形图(){
int[][]面积=新int[M][N];

对于(int i=0;i,错误似乎在告诉您,在某些情况下,您的代码将抛出异常。显然,竞争对手希望您避免这种情况

一个简单的解决方案是将整个
main
方法包装在try/catch中,并在末尾发布一些消息

public static void main(String[] args){
  try {
    // original code
  } catch (Throwable t) {
    t.printStackTrace();
  }
}

更好的解决方案是找出在什么情况下抛出异常,并尽可能避免。这需要对代码和潜在问题进行分析——这是我不会为您做的。

那么,它说您需要在哪里抛出异常?它真的说“某种异常”吗?您能告诉我们确切的错误消息吗?@msrd0 NZEC代表非零退出代码。对于C用户,如果主方法没有return 0;语句,则会生成此错误。其他语言,如Java/C++如果抛出异常,则可能会生成此错误。@msrd0这是我得到的错误。您可以找到codechef使用的编译器。它表示se
javac-1.7.0_25
适用于所有Java程序。您可以尝试使用jdk7编译程序