Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में FileNotFoundException पकड़ें

C# में FileNotFoundException पकड़ना

FileNotFoundException तब थ्रो होता है जब फाइल सिस्टम पर निर्दिष्ट पाथ पर फाइल नहीं मिलती। यह IOException से विरासत में मिला है।

उदाहरण: फाइल न मिलने पर एक्सेप्शन

using (StreamReader reader = new StreamReader("nonexistent.txt")) {
    string content = reader.ReadToEnd(); // FileNotFoundException यहां थ्रो होगा
}

सही हैंडलिंग: try-catch

using System;
using System.IO;

class Program {
    static void Main() {
        string path = "new.txt"; // यह फाइल मौजूद नहीं है
        
        try {
            using (StreamReader reader = new StreamReader(path)) {
                string content = reader.ReadToEnd();
                Console.WriteLine(content);
            }
        }
        catch (FileNotFoundException ex) {
            Console.WriteLine("File Not Found!");
            Console.WriteLine($"Path: {ex.FileName}");
            Console.WriteLine($"Message: {ex.Message}");
        }
        catch (DirectoryNotFoundException ex) {
            Console.WriteLine("Directory not found: " + ex.Message);
        }
        catch (IOException ex) {
            Console.WriteLine("I/O Error: " + ex.Message);
        }
    }
}

आउटपुट

File Not Found!
Path: new.txt
Message: Could not find file 'C:\Current\Directory\new.txt'.

बेहतर दृष्टिकोण: पूर्व-जांच (Defensive Check)

if (File.Exists(path)) {
    using (var reader = new StreamReader(path)) {
        // पढ़ें
    }
} else {
    Console.WriteLine("File does not exist.");
}

File.Exists vs Try-Catch

दृष्टिकोणपक्षविपक्ष
File.Existsसरल, एक्सेप्शन ओवरहेड नहींTOCTOU रेस कंडीशन (चेक के बाद फाइल डिलीट हो सकती है)
try-catchएटॉमिक, रेस-फ्रीएक्सेप्शन ओवरहेड (लेकिन दुर्लभ के लिए ठीक)

अन्य सामान्य फाइल एक्सेप्शन्स

  • DirectoryNotFoundException: डायरेक्टरी पाथ अमान्य।
  • UnauthorizedAccessException: परमिशन नहीं।
  • PathTooLongException: पाथ > 260 कैरेक्टर्स (Windows डिफ़ॉल्ट)।
  • DriveNotFoundException: ड्राइव मौजूद नहीं।

निष्कर्ष

फाइल ऑपरेशन्स में हमेशा try-catch उपयोग करें। FileNotFoundException को विशिष्ट रूप से पकड़ें ताकि यूजर-फ्रेंडली मैसेज दे सकें। using स्टेटमेंट से स्ट्रीम ऑटो-क्लोज़ होती है।

  1. Windows 10  Entry Point Not Found  त्रुटि को जल्दी और सुरक्षित रूप से हल करें Windows 10 'Entry Point Not Found' त्रुटि को जल्दी और सुरक्षित रूप से हल करें

    क्या आपको अपने Windows पर Entry Point Not Found त्रुटि संदेश मिल रहा है? Windows 10 में Entry Point Not Found एक त्रुटि है जिसने दुनिया भर में कई लोगों को परेशान किया है। यह त्रुटि आपके कंप्यूटर में लापता या भ्रष्ट DLL फाइलों के कारण होती है। DLL फाइलें डायनामिक लिंक लाइब्रेरी होती हैं जिनमें उपयोगी

  1. 404 Not Found Error को ठीक करें: त्वरित समस्या निवारण गाइड 404 Not Found Error को ठीक करें: त्वरित समस्या निवारण गाइड

    हर इंटरनेट उपयोगकर्ता वेब पर सर्फिंग करते समय कभी न कभी कुछ सामान्य त्रुटि संदेशों का सामना करता है। कुछ सामान्य प्रकार की इंटरनेट त्रुटियां हैं: 502 Bad Gateway Error, Aw Snap Error, ERR_SSL_PROTOCOL_ERROR और 404 Not Found Error। इन सभी त्रुटियों में से, Error 404 काफी आम है जिसका सामना हम किसी वेब

  1. त्वरित समाधान: विंडोज पीसी पर  ऑपरेटिंग सिस्टम नहीं मिला  बूट त्रुटि हल करें त्वरित समाधान: विंडोज पीसी पर 'ऑपरेटिंग सिस्टम नहीं मिला' बूट त्रुटि हल करें

    Missing operating system या boot device not found, please install an operating system on your hard disk, त्रुटि संदेश एक काली स्क्रीन पर दिखाई देते हैं। इसका मतलब है कि आप अपने पीसी को बूट नहीं कर सकते। यहां no operating system found असामान्य समस्या को ठीक करने का तरीका बताया गया है। आम तौर पर, उ