2012-04-20 14 views
0

請告訴我如何從JavaScript退出退出(我的硬盤上運行它)當我使用if語句。後續的代碼不起作用:如何從腳本中使用的if else

if (fso.FileExists("C:\\EXT\\file.txt")) //check if there file in the folder 
      { 
    log.WriteLine(file_exist_time + " - file exists C:\\EXT\\ "); 
      } 
    else 
      { 
      log.WriteLine("There is no file in C:\\EXT\\"); 

          function exit() 
            { 
           throw ('Script Exit'); 
            } 

     } 

非常感謝您!

+3

爲什麼不直接在日誌調用後執行'throw'?現在你只需定義一個名爲'exit'的函數,但是你永遠不會調用它。 – 2012-04-20 07:40:58

+1

「返回」是否工作? – Tobi 2012-04-20 07:40:59

+0

扔('退出')不起作用。如果你把'return'在 – May12 2012-04-20 07:46:29

回答

0

如何:

if (fso.FileExists("C:\\EXT\\file.txt")) //check if there file in the folder 
{ 
    log.WriteLine(file_exist_time + " - file exists C:\\EXT\\ "); 
} 
else 
{ 
    log.WriteLine("There is no file in C:\\EXT\\"); 
    return; 
    // Or possibly throw("Exit"); 
} 

換句話說,不要把throwreturn在一個單獨的功能,特別是如果你不調用該函數。

+0

return and throw(「Exit」)不起作用。它的工作原理:else { log.WriteLine(「C:\\ EXT \\」中沒有文件); \t \t \t破頂; \t \t \t \t \t \t \t } – May12 2012-04-20 08:05:10

1

爲什麼在這裏使用一個函數的定義?你甚至不會調用這個函數。你可以簡單地這樣做:

if (fso.FileExists("C:\\EXT\\file.txt")){ //check if there file in the folder 
    log.WriteLine(file_exist_time + " - file exists C:\\EXT\\ "); 
} 
else{ 
    log.WriteLine("There is no file in C:\\EXT\\"); 
    throw('Script Exit'); 
} 

然而,throw本身並不停止執行功能,它只能移動到你的catch語句來。閱讀更多關於exception handling