3.0 University logo
  • Home
  • About us
  • All Courses
    • Cybersecurity Programs
      • Certified Ethical Hacker (CEH v13)
      • Certified SOC Analyst
      • Certified Penitration Testing Professional
      • Computer Hacking Forensic Investigator
      • Certified Cybersecurity Technician (CCT)
      • Certified AI Program Manager
      • Certified Offensive AI Security Professional
      • Certified Responsible AI Governance & Ethics Professional
      • Artificial Intelligence Essentials
    • Crypto Market Programs
    • Blockchain & Web3 Programs
      • Digital Assets Trading & Analysis Program
      • Certified Web3 Strategy & Growth Specialist
      • Certified Web3 Governance & Compliance Expert
      • Full Stack Blockchain Developer Program
      • Private Blockchain Developer Program
      • Public Blockchain Developer Program
    • IGM x IIG Programs
      • Jewellery Design Executive Program
      • Gems & Diamond Specialist Program
      • Jewellery Business Specialist Program
  • Schools
    • School of Decentralized Economics
    • School of Cyber Resilience
    • School of Intelligent Systems
    • School of Design Thinking
  • Partners
    • Certification & Knowledge Partner
    • Academic Partner
    • Hiring Partner
    • Delivery Partner
    • Affiliate Partner
    • Hybrid Center Partner
  • Blog
  • Home
  • About us
  • All Courses
    • Cybersecurity Programs
      • Certified Ethical Hacker (CEH v13)
      • Certified SOC Analyst
      • Certified Penitration Testing Professional
      • Computer Hacking Forensic Investigator
      • Certified Cybersecurity Technician (CCT)
      • Certified AI Program Manager
      • Certified Offensive AI Security Professional
      • Certified Responsible AI Governance & Ethics Professional
      • Artificial Intelligence Essentials
    • Crypto Market Programs
    • Blockchain & Web3 Programs
      • Digital Assets Trading & Analysis Program
      • Certified Web3 Strategy & Growth Specialist
      • Certified Web3 Governance & Compliance Expert
      • Full Stack Blockchain Developer Program
      • Private Blockchain Developer Program
      • Public Blockchain Developer Program
    • IGM x IIG Programs
      • Jewellery Design Executive Program
      • Gems & Diamond Specialist Program
      • Jewellery Business Specialist Program
  • Schools
    • School of Decentralized Economics
    • School of Cyber Resilience
    • School of Intelligent Systems
    • School of Design Thinking
  • Partners
    • Certification & Knowledge Partner
    • Academic Partner
    • Hiring Partner
    • Delivery Partner
    • Affiliate Partner
    • Hybrid Center Partner
  • Blog
    Login
    ₹0.00 0 Cart

    Learn Articles

    • Home
    • Learn Articles

    Python Exception Handling, Threading, GIL and Memory Management Explained

    • Posted by 3.0 University
    • Date August 25, 2026
    • Comments 0 comment

    Exception handling in Python is the process of catching and responding to runtime errors so your program does not crash unexpectedly. You use the try-except-finally block to wrap risky code, catch specific error types, and clean up resources regardless of what happens. It is one of the first things a hiring manager will probe in a Python interview.

    • try-except-finally lets you catch errors, handle them gracefully, and always run cleanup code.
    • You can define custom exceptions by subclassing Python’s built-in Exception class.
    • The GIL (Global Interpreter Lock) prevents true parallel execution of CPU-bound threads in CPython.
    • Reference counting plus a cyclic garbage collector handle most of Python’s memory automatically.
    • Pickling serialises Python objects to bytes, but loading untrusted pickled data is a serious security risk.

    Handling Errors with try, except, else and finally

    What is exception handling in Python? At its core, exception handling in Python is a structured way to deal with things that go wrong at runtime, like a missing file, a bad network call, or a division by zero. Without it, one unexpected error terminates the entire program. With it, you decide what happens next.

    The try-except-else-finally Pattern

    The try block holds the code you are not 100% sure about. The except block runs only if an exception is raised. The else block runs only if no exception occurred, which is perfect for code that should execute after a successful operation. The finally block runs no matter what, making it ideal for closing files or releasing connections.

    Here is a realistic file-reading example that shows all four clauses working together:

    • try: open and read a config file.
    • except FileNotFoundError: log that the file is missing and fall back to defaults.
    • except PermissionError: alert the user that access was denied.
    • else: parse the file content, since we know it loaded cleanly.
    • finally: close the file handle whether reading succeeded or failed.

    Catching broad exceptions with a bare except: clause is a bad habit. Always catch the most specific exception type you expect. Catching Exception as a last resort is acceptable; catching everything silently is not.

    Raising Custom Exceptions

    Sometimes Python’s built-in exceptions do not describe your domain accurately enough. If you are building a payment gateway integration, a generic ValueError does not tell you much. That is where custom exceptions come in.

    You create one by subclassing Exception:

    class InsufficientFundsError(Exception): pass

    Then you raise it with a meaningful message wherever the business rule breaks. The caller can catch InsufficientFundsError specifically without interfering with unrelated exceptions. This pattern is standard in any production Python codebase. Interviewers at companies like Infosys, Wipro, Razorpay, and Swiggy expect you to know it cold. According to the JetBrains Python Developers Survey 2023, 78% of Python developers use custom exception classes in production projects, making it one of the most widely adopted intermediate Python patterns.

    Context Managers and the with Statement

    A context manager is essentially a cleaner way to handle setup and teardown. When you open a file with with open('data.csv') as f:, Python automatically calls the file’s __exit__ method when the block ends, even if an exception fires. It is the same guarantee as finally, but with far less boilerplate. Python exception handling best practices in Indian enterprise codebases at firms like Flipkart and Zepto consistently favour context managers over manual try-finally patterns for resource management.

    Threading, Multiprocessing and the GIL

    Does Python support multithreading? Yes, through the built-in threading module. But there is a catch that trips up most beginners, and it is called the GIL.

    What is the GIL in Python?

    The GIL, or Global Interpreter Lock, is a mutex in CPython that allows only one thread to execute Python bytecode at a time. It was introduced to simplify memory management and make CPython’s object model thread-safe without fine-grained locking on every object. The trade-off is that CPU-bound threads do not actually run in parallel, even on a multi-core machine.

    The GIL is specific to CPython, the reference implementation. Alternative implementations like Jython and IronPython do not have it. The CPython core team has been actively working on making the GIL optional since Python 3.12, with the no-GIL build available as an experimental option as of Python 3.13.

    Choosing the Right Concurrency Tool in Python
    Scenario Best Tool Why
    I/O-bound tasks (API calls, file reads) threading module GIL is released during I/O waits, so threads do help
    CPU-bound tasks (image processing, ML training) multiprocessing module Each process gets its own GIL and memory space
    High-concurrency async I/O (web servers) asyncio Single-threaded event loop, no GIL contention at all
    True parallel CPU work across cores concurrent.futures ProcessPoolExecutor Spawns worker processes, bypasses GIL entirely

    The Stack Overflow Developer Survey 2024 found that Python ranked as the most popular language for the third consecutive year, used by 51% of professional developers surveyed. Most of them encounter GIL confusion within their first year of writing concurrent code. In India specifically, NASSCOM’s 2023 Tech Talent Report noted Python as the top language skill requested in backend and data engineering job postings across Bengaluru, Hyderabad, and Pune, making GIL fluency a genuine hiring differentiator.

    When Threading Still Works

    For I/O-bound work, the threading module is perfectly effective. When a thread waits on a network response or a disk read, CPython releases the GIL so another thread can run. Web scraping, calling external APIs, reading log files concurrently, these are all good fits for threading.

    If you are building data pipelines and want to go deeper on parallel processing patterns, the Learn: Big Data Analytics notes on 3.0 University cover distributed processing concepts that complement what you are learning here.

    Memory Management, Garbage Collection and Pickling

    How is Memory Managed in Python?

    Python manages memory automatically, which is a big reason beginners find it friendlier than C or C++. The primary mechanism is reference counting. Every Python object carries an internal counter that tracks how many references point to it. When that count drops to zero, CPython immediately deallocates the object’s memory.

    This works brilliantly for most cases. The problem is circular references: object A points to object B, and object B points back to object A. Neither counter ever reaches zero, so neither gets freed by reference counting alone.

    What is Garbage Collection in Python?

    Python’s garbage collector module handles this. It runs a cyclic collector that identifies groups of objects that reference each other but are unreachable from any active code. The collector uses a generational algorithm with three generations, where objects that survive collection are promoted to older generations and checked less frequently. This keeps the overhead low for long-lived objects.

    You can interact with it directly using import gc, call gc.collect() to force a sweep, or disable it entirely in performance-critical applications where you are confident no cycles exist. Instagram’s engineering team disabled Python’s cyclic garbage collector in their Django application and reported a 10% memory reduction, as documented in a 2017 Instagram Engineering blog post. This technique has since been adopted by several high-traffic Indian consumer apps to reduce server costs at scale.

    What is Pickling in Python?

    Pickling is Python’s built-in object serialisation system. The pickle module converts any Python object, a dictionary, a class instance, a list, into a byte stream you can store to disk or send over a network. Unpickling reverses that process.

    It is widely used in machine learning workflows. Scikit-learn models, for example, are routinely saved and loaded with pickle or its faster cousin joblib. A trained model built in a Bengaluru data centre can be pickled, shipped to a server in Hyderabad, and unpickled in milliseconds.

    The security caveat is non-negotiable: never unpickle data from an untrusted source. A malicious pickle file can execute arbitrary code on your machine the moment you call pickle.loads(). The Python documentation states this explicitly. For data exchange with external systems, use JSON or protobuf instead.

    If you are thinking about where Python fits in a broader AI or data science career, the 3.0 University piece on how to shift from data science to AI and ML walks through the skill stack you need in practical terms.

    These concepts are not just interview trivia. They determine whether your production code is stable, performant and safe. A developer who understands what is exception handling in Python picks the right error strategy. One who understands the GIL picks the right concurrency tool. One who understands garbage collection writes code that does not leak memory under load. One who knows pickle’s risks does not introduce a remote code execution vulnerability into their ML pipeline.

    If you want structured guidance through topics like this, the REACH learner community at 3.0 University connects you with peers and mentors working through the same material. For curated reading on Python, cybersecurity, and tech careers, the 3.0 University blog publishes practitioner-written content regularly.

    The next step this week: write a small Python script that opens a file, handles at least three specific exceptions, uses a context manager, and prints the reference count of an object using sys.getrefcount(). That one exercise will make the concepts in this article stick faster than any amount of reading.

    Whether you are a fresh graduate preparing for campus placements, a working professional upskilling for a backend role, or a career switcher looking to break into tech, building hands-on Python fluency is non-negotiable. Explore online certification courses at 3.0 University covering Cybersecurity, Ethical Hacking, Artificial Intelligence, Blockchain and Web3. The bootcamp training programs are designed around real-world projects and labs, not just theory, so you leave with a portfolio, not just a certificate.

    Frequently Asked Questions

    What is exception handling in Python?

    Exception handling in Python is the structured use of try-except-finally blocks to catch and respond to runtime errors without crashing the program. You wrap risky code in a try block, specify how to handle each error type in except clauses, and use finally for cleanup code that must always run, like closing files or releasing database connections.

    What is the GIL in Python?

    The GIL, or Global Interpreter Lock, is a mutex in CPython that allows only one thread to execute Python bytecode at a time. It protects internal object state but prevents CPU-bound threads from running truly in parallel. For CPU-heavy work, use the multiprocessing module instead. For I/O-bound tasks, the threading module still gives you real concurrency because the GIL is released during I/O waits.

    How is memory managed in Python?

    Python uses reference counting as its primary memory management strategy. Every object tracks how many references point to it, and when that count hits zero, the memory is freed immediately. A separate garbage collector handles circular references using a generational algorithm. Most developers never need to manage memory manually, but understanding this helps you write more efficient code.

    Does Python support multithreading?

    Yes, Python supports multithreading through the built-in threading module. It works well for I/O-bound tasks like network requests and file operations. For CPU-bound parallel work, the GIL limits thread performance, so the multiprocessing module or concurrent.futures.ProcessPoolExecutor is the right choice. Python 3.13 introduced an experimental no-GIL build that may change this in future.

    What is pickling in Python?

    Pickling is Python’s built-in serialisation mechanism. The pickle module converts Python objects into a byte stream for storage or transmission, and unpickling reverses it. It is commonly used to save trained machine learning models. The critical warning: never unpickle data from an untrusted source, as a malicious pickle file can execute arbitrary code on your system the moment it is loaded.

    Last updated: August 2026. Reviewed by the 3University editorial team.

    • Share:
    3.0 University

    Previous post

    Python File Handling: How to Read Excel, CSV, JSON and Text Files
    August 25, 2026

    Next post

    Python Projects for Beginners: Build a Game, Calculator, Chatbot or App
    August 25, 2026

    You may also like

    Free AI Certificate Course by Government of India
    FREE AI Course with Certificate Launched by Govt of India
    June 19, 2026
    Highest Paid Professions in India
    Highest Paid Profession in India
    June 12, 2026
    Cyber Security Course Eligibility
    Cyber Security Course Eligibility
    June 11, 2026

    Leave A Reply Cancel reply

    You must be logged in to post a comment.

    3.0 University is a pioneering academic initiative for creating a comprehensive knowledge ecosystem for emerging technologies. We have developed an in-house suite of course offerings for retail, institutional market participants and industry-at-large. 

    Facebook X-twitter Instagram Linkedin
    Quick Links
    • About us
    • Courses
    • Become a Partner
    • Contact Us
    • Blog
    • Learn
    Trending Courses
    • Certified SOC Analyst
    • Certified Ethical Hacker v13 Program
    • Certified Penitration Testing Professional
    • Full Stack Blockchain Developer
    • Certified AI Program Manager
    Policies
    • Privacy Policy
    • Terms and Conditions
    • Disclaimer
    • Refund Policy
    Contact Us
    FT Tower, CTS No. 256 & 257, Suren Road, Chakala, Andheri (E), Mumbai-400093 India.

    +91 8657961141

    support@3university.io

    Login with your site account

    Lost your password?

    Not a member yet? Register now

    Register a new account

    Are you a member? Login now

    Login with your site account

    Lost your password?

    Not a member yet? Register now

    Register a new account

    Are you a member? Login now

    Sign In

    Welcome back! Or create an account

    OR
    Forgot password?

    Need a new verification email?

    Don't have an account? Register

    Create Account

    Already have an account? Sign in

    OR

    Already have an account? Log in

    Reset Password

    Enter your email and we'll send you a reset link.

    ← Back to login

    Check Your Email

    Almost there!
    We have sent a verification link to your email address. Please check your inbox (and spam folder) and click the link to activate your account.

    Didn't receive the email? Enter your address to resend:

    Already verified? Sign in