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 8447961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:18:12+00:00 2026-06-10T10:18:12+00:00

The main thing is I don’t know the best practice of handling null pointers.

  • 0

The main thing is I don’t know the best practice of handling null pointers.

here is the code:

int GetSomeVal()
{
    if(ptr)
    {
        return *ptr
    }
    else
    {
        // What here?
        // Should I return 0? or -1 or throw an exception?
    }
}

I got this question because in google’s c++ style-guide, it doesn’t recommand us to use exceptions, and this may be another topic.

If I should use exceptions, does that mean I have to design an exception class an throw it?
If I should not use exceptions, what is the best practice?

  • 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-10T10:18:14+00:00Added an answer on June 10, 2026 at 10:18 am

    If it makes sense for ptr to be NULL, you shouldn’t throw an exception. Exceptions are used to indicate something went wrong.

    If ptr is NULL because something went wrong, by all means, throw an exception.

    Google’s code style is to be used by Google. They prohibit lots of things (like reference arguments, exceptions, etc.) that are normally used in any high-level OO language. You shouldn’t obey it unless you work for Google.

    You could design your own exception class, derive it from std::exception, and throw that from the code.

    Also, from your code, ptr is a member. If it was supposed to get initialized in the constructor and it didn’t, perhaps you should throw the exception from the constructor. There’s no use in having an un-usable object.

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

Sidebar

Related Questions

I have a main NSMutableDictionary that contains a collection of others NSMutableDictionary. The thing
I am learning Java, theres one thing I do not understand.. in the main
I have written a simple Hello World program. #include <stdio.h> int main() { printf(Hello
Jquery bind is amazing, but I don't know in what order the binding happens.
I'm investigating whether I can use webkit-sharp for my application. The main thing I
The main thing is that I have some ideas in my mind and we
I've read the Google docs on sitemap formats but there's one thing they don't
I'm thinking in learning Zend or Symfony frameworks but I don`t know which one
I don't know why but the bit fields assignment is not working as expected.
So main think with good reduce phase is good partition distribution. But for example

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.