Skip to main content

Command Palette

Search for a command to run...

Handling

Published
2 min read
Handling
A

I am gonna to start my journey as a full stack web developer.

Exception Handling

Error: Error can occure due to mistake in the code that prevent it from running. These can be syntax errors or indentation error or logical errors for example missing an input , unclosed bracket etc.

Exception:
Exceptions are unexpected events or errors that occur during the execution of a program, which disrupts the normal flow of program.

Exception Handling:

Python Exception Handling allows a program to gracefully handle unexpected events (like invalid input or missing files) without crashing using try, except, else, and finally blocks. Instead of terminating abruptly, Python lets you detect the problem, respond to it, and continue execution when possible.

try: Wraps the code that can occur exception or might fail.

exception: Handle except if error occurs like ZeroDivisionError.

else: Executes only if no exception occurs in try.

finally: Runs code no matter what, whether there’s an exception or not. It useful for cleanup tasks like closing files.

File Handling

File: Any name with extension like .py, .txt, .mp3 etc. is called file.

File Handling: It means performing CRUD operation like creating, updating, reading, deleting, and closing it through a programming interface.\

Opening a File: we can use open(‘filena,e’ , ‘mode’) function to open file. Mode can be read, write, append etc. for example f = (‘file.txt’, ‘r’)

Closing a File: The file.close() method closes the file and releases the system resources. If the file was opened in write or append mode, closing ensures that all changes are properly saved.

ModeDescription
"r"Read (default) – file must exist
"w"Write – creates new file or overwrites existing
"a"Append – creates file if not exists, adds to end
"x"Create – fails if file exists
"t"Text mode (default)