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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:23:08+00:00 2026-06-08T19:23:08+00:00

I want to do some exception handling. I plan on using the __LINE__ and

  • 0

I want to do some exception handling. I plan on using the __LINE__ and __FILE__ macros.

I have some header Vectors.hpp in which I implement some class for a vector structure. In this class I implement the operator [] and I want to throw an exception each time this operator is used with an out of bounds index. I test this class in some source test.cpp. I want then to be able to see the exact line in test.cpp where this happened.

However I know that the __LINE__ macro is disabled every time you include some header, so what I got is the line in Vectors.hpp where I handle the exception and not the line in test.cpp. Is there a nice way to get around this? Or, how would one implement his own __LINE__ macro?

  • 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-08T19:23:09+00:00Added an answer on June 8, 2026 at 7:23 pm

    The __LINE__ macro is never disabled. It is expanded where you write it. There are two ways to write code (more precisely, there are two ways to produce tokens):

    • write it manually
    • use the preprocessor to write code

    If you have it is some file foo.cpp like this (just exemplary, that’s very bad code actually)

    class Foo {
    public:
        Frob operator[] (size_t) { throw __LINE__ }
    };
    

    then __LINE__ is always 3 and __FILE__ is always foo.cpp.

    That’s because the macros are expanded where they are used. The solution would be to find a way to get them expanded where you want it, and the only way to do so is to define another macro:

    #define safe_subscript(foo, index) \
          try {foo[index];} \
          catch(...) { std::cout << __LINE__ << '\n'; }
    
    ....
    safe_subscript(foo, 256);
    

    But as you see, this leads to quite ugly code and workarounds.

    Real Solution: Just throw an exception upon out of bounds (throw std::out_of_range), or do it like the standard library:

    T& operator[] (size_type i) { return store_[i]; }
    T& at (size_type i) { if (i>size_) throw std::out_of_range("crap");
                          return store_[i]; }
    
    T operator[] (size_type i) const { return store_[i]; }
    T at (size_type i) const { if (i>size_) throw std::out_of_range("crap");
                               return store_[i]; }
    

    If your user receives an exception, he is ought to debug where he/she/it made a programming error.

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

Sidebar

Related Questions

I want to know the global mechanism for exception handling. Like I do have
I'm trying to implement exception handling in ASP.NET MVC3 using the HandleError attribute. The
I'm working on the exception handling layer for my application. I have read few
I've got a WCF service that's using Enterprise Library for its Exception Handling and
I have some trouble handling custom C++ exceptions when calling from Cython. My situation
I was writing some code, and I notice a pattern in the exception handling
We're using Enterprise Library 4.1 for logging (and exception handling/cryptography). Does anyone know a
I have a few doubts regarding exception handling in iPhone. Here are they: Suppose
Possible Duplicate: Javascipt exception handling is it possible to add some global exception listener
I am currently asking myself some questions about exception handling and eventhandlers, and i

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.