GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Python Systemexit Exception with Example - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Python Systemexit Exception with Example

Last Updated : 1 Jul, 2026

A SystemExit exception occurs when a Python program is intentionally terminated using the sys.exit() function. Unlike most exceptions, SystemExit is not usually caused by an error in the code. Instead, it is used to stop the execution of a program in a controlled manner.

When sys.exit() is called, Python raises a SystemExit exception internally. If this exception is not handled, the program terminates immediately.

Python
import sys
sys.exit("Program terminated")

Output

Program terminated

Explanation: sys.exit() function raises a SystemExit exception with the message "Program terminated". Since the exception is not handled, the program stops execution.

Common Causes

1. Explicit Program Termination: most common cause of SystemExit is calling sys.exit() directly.

Python
import sys

def exit_program():
    sys.exit("Exiting the program")

exit_program()

Output

Exiting the program

Explanation: function calls sys.exit(), which raises a SystemExit exception and immediately terminates the program.

2. Exiting Based on a Condition: Sometimes a program needs to stop when a certain condition is met.

Python
import sys
age = -5

if age < 0:
    sys.exit("Age cannot be negative")

print("Valid age")

Output

Age cannot be negative

Explanation: Since the age is invalid, sys.exit() is executed and the program terminates before reaching the print() statement.

3. Termination Triggered by Signals: In some applications, a termination signal from the operating system can eventually lead to a SystemExit exception.

Python
import signal

def handler(signum, frame):
    raise SystemExit("Program terminated by signal")

signal.signal(signal.SIGINT, handler)

Output

Program terminated by signal

Explanation: When the user presses Ctrl+C, the signal handler executes and raises a SystemExit exception to terminate the program gracefully.

Handling SystemExit Exception

Although SystemExit is normally used to stop a program, it can be caught using a try-except block if additional actions need to be performed before termination.

1. Catching SystemExit: to perform additional actions before the program exits. By catching the SystemExit exception, we can log messages, save information or execute custom code instead of allowing the program to terminate immediately.

Python
import sys

try:
    sys.exit("Exiting the program")
except SystemExit as error:
    print("Caught SystemExit:", error)

print("Program continues")

Output
Caught SystemExit: Exiting the program
Program continues

Explanation: SystemExit exception is caught by the except block, preventing the program from terminating immediately.

2. Handling SystemExit Inside a Function: When sys.exit() is used inside a function, we can handle the SystemExit exception within that function itself. This approach keeps the termination logic localized and allows the rest of the program to continue executing if needed.

Python
import sys

def exit_safely(message):
    try:
        sys.exit(message)
    except SystemExit as error:
        print("Caught SystemExit:", error)

exit_safely("Exiting the program")
print("Program continues")

Output
Caught SystemExit: Exiting the program
Program continues

Explanation: function catches the SystemExit exception internally and allows the remaining code to execute normally.

3. Performing Cleanup Before Exit: In real-world applications, it is often useful to save data or release resources before terminating.

Python
import sys

try:
    print("Saving data...")
    sys.exit("Program terminated")
except SystemExit as error:
    print(error)
finally:
    print("Cleanup completed")

Output
Saving data...
Program terminated
Cleanup completed

Explanation: finally block executes even when SystemExit is raised, making it a suitable place for cleanup operations such as closing files or database connections.

Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.