错误捕捉/错误处理

时间:2018-07-18 10:47:40

标签: java error-handling

我需要在else中捕获或打印错误。

下面是我的代码:

private static void runProcess(String command) throws Exception {
    Process pro = Runtime.getRuntime().exec(command);
    printLines(command + " stdout:", pro.getInputStream()); // For RUN output only
    pro.waitFor();
    if (pro.exitValue() == 0) 
    {
        System.out.println("Status: COMPILATION SUCCESSFULL!!");
        // sendFile(); // to code checker
        sendFile();
        System.out.println("File Sent!");

    } else
        System.out.println("Syntax Error Found!");
}

1 个答案:

答案 0 :(得分:0)

也许是这样吗?

private static void runProcess(String command) throws Exception {
try{
    Process pro = Runtime.getRuntime().exec(command);
    printLines(command + " stdout:", pro.getInputStream()); // For RUN output only
    pro.waitFor();
    if (pro.exitValue() == 0) {
        System.out.println("Status: COMPILATION SUCCESSFULL!!");
        // sendFile(); // to code checker
        sendFile();
        System.out.println("File Sent!");
    }

}catch(Exception e){
//Your exception message here, handle it as you want
  System.out.println(e.getMessage());
  System.out.println("Syntax Error Found!");
} 

}