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

  • SEARCH
  • Home
  • 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 8062115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:33:35+00:00 2026-06-05T10:33:35+00:00

So I was writing some code and I noticed that apart from syntactical, type,

  • 0

So I was writing some code and I noticed that apart from syntactical, type, and other compile-time errors, C++ does not throw any other exceptions. So I decided to test this out with a very trivial program:

#include<iostream>

int main() {
    std::count<<5/0<<std::endl;
return 1
}

When I compiled it using g++, g++ gave me a warning saying I was dividing by 0. But it still compiled the code. Then when I ran it, it printed some really large arbitrary number. When I want to know is, how does C++ deal with exceptions? Integer division by 0 should be a very trivial example of when an exception should be thrown and the program should terminate.

Do I have to essentially enclose my entire program in a huge try block and then catch certain exceptions? I know in Python when an exception is thrown, the program will immediately terminate and print out the error. What does C++ do? Are there even runtime exceptions which stop execution and kill the program?

  • 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-05T10:33:37+00:00Added an answer on June 5, 2026 at 10:33 am

    There are runtime exceptions, but not everything that’s “wrong” results in a runtime exception being thrown. For example, acccessing an array out of bounds or dereferencing a null pointer is simply “undefined behaviour” – meaning anything at all can happen. Division by zero also falls into the “undefined” category.

    The rationale for some operations resulting in “undefined behaviour” rather than an exception is efficiency. Suppose an out-of-bounds array access required an exception to be thrown. Then the compiler would have to generate code for each array access to checks whether it’s out of bounds, and if so, throw an exception. That’s a lot of checking, most of which is unnecessary. Instead, what compilers do is just generate the instruction for the element access assuming it is within bounds. If it happens to be out of bounds, then whatever happens (e.g. segmentation fault) happens. If you want a check to be performed, you can always code it explicitly.

    This makes C++ more powerful than languages that always do checks (e.g. Java or python) because you can choose when you want a check done, and when you don’t. (On the other hand, it makes C++ less safe than Java or python. It’s a trade-off).


    As for what happens when an exception is thrown but not caught anywhere, typically compiler implementations will print an error message containing the exception’s what(). In your example that’s not applicable because no runtime exception is being thrown.

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

Sidebar

Related Questions

I was just writing some simple code and I noticed that using document.writeln doesn't
I'm writing some code in Visual Basic 6 and I have noticed that I
I'm looking into writing simple graphics code in Android and I've noticed some synchronized()
I am currently writing some code that can access a separate workbook through an
I'm writing some code to load and parse HTML docs from the web. I'm
I'm writing some code (just for fun so far) in Python that will store
I'm writing some code that has a lot of substitution. There's a list<char> productions
I am writing some code to type strings using the Robot class. Everything is
I am writing some code that connects to a website, and using C#, and
i am writing some code in vb.net that will be generating a pdf file.

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.