- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to capture file not found exception in C#?
The file not found exception is raised when you try to find a file that does not exist.
Let’s say I have set a file in StreamReader, “new.txt” that does not exist. If you will try to access it using StreamReader(to read it), it will throw FileNotFoundException −
using (StreamReader sReader = new StreamReader("new.txt")) { sReader.ReadToEnd(); }
To handle it, you need to use try and catch −
Try { using (StreamReader sReader = new StreamReader("new.txt")) { sReader.ReadToEnd(); } }catch (FileNotFoundException e) { Console.WriteLine("File Not Found!"); Console.WriteLine(e); }
- Related Articles
- How to capture file not found exception in Java?
- How to capture null reference exception in C#?
- How to capture divide by zero exception in C#?
- How to capture out of memory exception in C#?
- How to capture index out of range exception in C#?
- How to capture divide by zero exception in Java?
- How to capture and print Python exception message?
- How to capture an exception raised by a Python Regular expression?
- How to capture out of array index out of bounds exception in Java?
- How to resolve exception Element Not Interactable Exception in Selenium?
- HTML file input control with capture and accept attributes is not working correctly
- Which exception is raised when an element is not found in an HTML DOM using XPath in Selenium?
- How to capture screenshots in Selenium?
- How to capture video from default camera in OpenCV using C++?
- How to capture video from other cameras in OpenCV using C++?

Advertisements