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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:43:17+00:00 2026-05-30T19:43:17+00:00

I’m just learning C++, and would like to throw an exception, but then the

  • 0

I’m just learning C++, and would like to throw an exception, but then the result of my function would be undefined???

 std::vector<myStruct> extract_notworking(std::vector<myStruct>& avec){
        std::vector<myStruct> result;

        if (avec.size() == 0)
            //throw domain_error("Cannot operate on empty vector!");
            //Cannot use exception for it would yield undefined result
            return result;

        //do something here
        //...
        return result;
    }

What should I do? Return an empty vector? What would happen if I threw the exception to the receiver of the return value?

  • 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-30T19:43:19+00:00Added an answer on May 30, 2026 at 7:43 pm

    When you throw an exception, the function halts there and execution jumps to wherever the exception was caught. Your function doesn’t return anything because the function doesn’t return at all.

    You can just do

    if (avec.empty())
        throw domain_error("Cannot operate on empty vector!");
    

    And your function will exit there.

    Note that you don’t need to be concerned about the return value (“How can a function not return anything?” etc) because you can’t access the return value of a function that threw (and did not catch) an exception even if you try.

    So for instance, if you do

    try {
        std::vector<myStruct> vec;
    
        std::vector<myStruct> retval = extract_notworking(vec);
    
        print_vector(retval); // this line is only executed if extract_networking
                              // does not throw an exception
    } catch (const domain_error& e) {
        // we can't access retval here so no worries
    }
    

    You can only access retval if the function returns properly (i.e. does not throw). In the example, your function will throw because vec is empty, so print_vector will never be called.

    Even if you do this:

    std::vector<myStruct> retval;
    
    try {
        std::vector<myStruct> vec;
    
        retval = extract_notworking(vec);
    
        print_vector(retval);
    } catch (const domain_error& e) {
        // we can access retval here but the assignment never happened
    }
    

    Since the function did not return, the assignment of its return value to retval did not happen, and retval is still a perfectly normal default-constructed vector that you can use freely. So in that example, retval is not assigned to and retval is not printed, because extract_networking threw an exception and execution jumped into the catch block before those two things could happen.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
I would like to run a str_replace or preg_replace which looks for certain words
I want to construct a data frame in an Rcpp function, but when I
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.