如何在 C# 中捕獲檔案未找到異常?
當你嘗試查詢一個不存在的檔案時將引發檔案未找到異常。
假設我在 StreamReader,中設定了一個不存在的檔案“new.txt”。如果你嘗試使用 StreamReader(讀取檔案)訪問它,將引發 FileNotFoundException -
using (StreamReader sReader = new StreamReader("new.txt")) {
sReader.ReadToEnd();
}要處理它,你需使用 try 然後 catch -
Try {
using (StreamReader sReader = new StreamReader("new.txt")) {
sReader.ReadToEnd();
}
}catch (FileNotFoundException e) {
Console.WriteLine("File Not Found!");
Console.WriteLine(e);
}
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP