Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8012023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:12:09+00:00 2026-06-04T19:12:09+00:00

What is the difference between calling sys.exit() and throwing an exception in Python? Let’s

  • 0

What is the difference between calling sys.exit() and throwing an exception in Python?

Let’s say I have a Python script which does the following:

  1. open a file
  2. read lines
  3. close it

If the file doesn’t exist or an IOException gets thrown at runtime, which of the options below makes more sense?

  1. no except/catch of the exception, if exception occurs, it fails out (which is expected behaviour anyway)
  2. except/catch the exception, logs the error message, throw customized exception by myself, fails out.
  3. in an except IOException block, exit with an error message e.g. sys.exit("something is wrong")

Does option 3 kill the process while 1 and 2 do not? What’s the best way to handle the Python exceptions given that Python doesn’t have a checked exception like Java (I am really a Java developer ^_^)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:12:11+00:00Added an answer on June 4, 2026 at 7:12 pm

    sys.exit raises a SystemExit itself so from a purely technical point of view there’s no difference between raising that exception yourself or using sys.exit. And yes you can catch SystemExit exceptions like any other exception and ignore it.

    So it’s just a matter of documenting your intent better.

    PS: Note that this also means that sys.exit is actually a pretty bad misnomer – because if you use sys.exit in a thread only the thread is terminated and nothing else. That can be pretty annoying, yes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one clarification What is the difference between calling a function through function
In python, is there a difference between calling clear() and assigning {} to a
I was wondering what's the difference between calling SendMessage (which blocks) and calling PostMessage
Could you explain to me what the difference is between calling python -m mymod1
What is the difference between calling AudioManager.setRingerMode to calling AudioManager.setStreamMute(AudioManager.STREAM_VOICE_CALL, ...) What does the
Does anyone know what's the difference between using breakpoints and calling DebugBreak() func. for
Is there any difference between calling len([1,2,3]) or [1,2,3].__len__() ? If there is no
what is the difference between calling a mapreduce job from main() and from ToolRunner.run()
I'm wondering what's the actual difference between calling Thread.Sleep(1) and calling SwitchToThread (if we
Given a BehaviorSubject, what is the practical difference between calling all of these different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.