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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:43:24+00:00 2026-05-24T11:43:24+00:00

I am writing some library functions that needs to do some error reporting. I

  • 0

I am writing some library functions that needs to do some error reporting. I want to use exceptions rather than return values.

I wrote my functions that throws int exceptions.

Eg:

if(strm->atEnd()){
    // unexpected end of stream
    throw -2;
}

My question is, is this method OK? Or should I throw a exception derived from std::exception?

In what way is throwing a std::exception better? (Other than being able to use catch(std::exception &e))

Is throwing int exceptions bad practice? (all throw int values are documented in doxygen comments)

I can’t find any reason as to why (in this case) should I throw an object.

  • 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-05-24T11:43:25+00:00Added an answer on May 24, 2026 at 11:43 am

    I would throw an exception based on std::exception.

    throw std::runtime_error("unexpected end of stream")
    

    I find this easier to catch, log, et cetera. It also allows the removal of a comment and a magic number from the code.

    This message can then be sent to the end-user to give them a hope of fixing the problem.

    Users and library consumers can’t read the comments in code, and they are unlikely to know what ‘-2’ means.

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

Sidebar

Related Questions

I'm writing a library that contains some hash functions. I want one of the
I am writing a library in C++ and have some functions that work with
I'm writing some data acquisition software and need a gui plotting library that is
I'm writing a customized reflective library for some specialized custom domain logic, and that
While making some final tests of a class-library that I'm writing for Windows Mobile
I have a library that I created with some business logic that includes writing
I am writing some code for a web app that regularly needs to filter
I'm writing some unit tests for a Python library and would like certain warnings
I'm writting a program in c# that uses a C++ library, and for some
Writing some test scripts in IronPython, I want to verify whether a window is

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.