
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Catch EOFError Exception in Python
EOFError is commonly seen when a program tries to read input, but there is no data left to read. This can happen, for example, when input is redirected from a file or when the user provides no input and presses Ctrl+D (Unix) or Ctrl+Z (Windows).
The best way to catch EOFError is by using the try-except block. Below are various ways to handle EOFError properly in Python -
- Using try-except block with EOFError
- Using try-except-else block
- Using try-except-finally block
Using try-except Block with EOFError
In Python, the input() function raises an EOFError if no input is given. You can catch this exception using a try-except block to prevent the program from crashing.
Example
In this example, we try to read user input. If no input is provided and EOF is reached, the EOFError is caught -
try: print("Enter something (or press Ctrl+D / Ctrl+Z to trigger EOF):") data = input() print("You entered:", data) except EOFError: print("No input provided. EOFError caught.")
The output of the above program depends on whether input is provided or not. If no input is given -
No input provided. EOFError caught.
Using try-except-else Block
The try-except-else block allows you to run a set of statements only when no exception occurs. If an EOFError is raised in the try block, the except block handles it. If no error occurs, the else block runs.
Example
In this example, if input is successfully received, the else block prints it. Otherwise, the except block catches the EOFError -
try: name = input("Enter your name: ") except EOFError: print("EOFError caught. No name entered.") else: print("Hello,", name)
If you press Ctrl+D (on Linux/Mac) or Ctrl+Z and Enter (on Windows) without typing a name, the output will be -
EOFError caught. No name entered.
Using try-except-finally Block
The finally block is used to run code no matter what happens in the try-except block, whether an exception occurs or not.
Example
In the following example, the finally block will always execute even if an EOFError is raised due to missing input -
try: age = input("Enter your age: ") except EOFError: print("Input not provided. EOFError caught.") finally: print("Program finished.")
Following is the output if no input is provided -
Input not provided. EOFError caught. Program finished.
Handling EOFError in Loops
When we are reading input values from a user inside a loop, an EOFError can occur if the input ends unexpectedly. This happens when the user presses Ctrl+D (on Linux/Mac) or Ctrl+Z (on Windows) when providing input.
If this error is not handled, the program may crash. To avoid this, we can use a try-except block to catch the EOFError and exit the loop safely.
Example
In the following example, the loop continuously reads user input line by line. If the user presses Ctrl+D (on Linux/Mac) or Ctrl+Z and Enter (on Windows), an EOFError is raised, which is caught to exit the loop -
print("Enter multiple lines (press Ctrl+D to end):") while True: try: line = input() except EOFError: print("End of input detected.") break else: print("You entered:", line)
Following is the output obtained when input ends using EOF -
Enter multiple lines (press Ctrl+D to end): End of input detected.