Program stops after exception handling

Asked 5 months ago, Updated 5 months ago, 12 views

The program stops after making an exception.

Specifically, the exception is to assume that try{}catch{} attempts to take the leading value from an empty concatenated list (head of concatenated list pointing to nullptr), where the program should have finished successfully.

lab9.exe has stopped working.A problem has occurred and the program is no longer working properly.
The program will be closed and Windows will notify you if there is a solution.

Normally, after completing the instructions (in this case, displaying istList error saying no link in the list 」), がProcess finished with exit code 0 」 appears and the program terminates, but the program stops without cProcess finished with exit code 0 」.
Is this what happens even with exception handling?

Current Source Code


#ifndef LAB9_LIST_H
#define LAB9_LIST_H

# include <string>
# include <exception>
using std::string;

class list_error:public std::exception {
    US>string errorMessage;
    list_error(string errorMessage=""): errorMessage(errorMessage){}
    virtual const char * what() const noxcept override
        return errorMessage.c_str();


template<typename T>
class Link
    T value;
    // constructor
    Link(T value, Link*next=nullptr): value(value), next(next){}
    // destructor
    // return value
    T getValue() const {return this ->value;}
    // return next
    Link*getNext()const {return this->next;}
    // set next
    void setNext(Link*next) {this->next=next;}

template<typename T>
class list
    Link <T>*head;


    T getHead()
        //throw exception
            row list_error("no link in the list");
        return head ->getValue();


# include "List.h"

using std::cout;
using std::endl;

int main()

    List <int>intList;

    // testing exception
        cout<<"Should have had an exception."<endl;
        cout<<"List error saying"<<ex.what()<<endl;
        cout<<"Cough something else"<<endl;

    return 0;


2022-09-30 14:10

1 Answers

It's been a while since you asked me a question, so I don't think it's going to matter anymore.

It looks like it's going to work, but I'm concerned that the constructor argument errorMessage in list_error is exactly the same as the member variable errorMessage.

If a member variable copy constructor is called errorMessage (errorMessage) and the member variable itself is passed to that argument, you might want to try to copy construct with an uninitialized string object as the source and cause an unauthorized reference or something along the way.

Why don't you shift the name of the constructor argument from the member variable?

Since it is common to want to give constructor arguments with the same name as member variables, I believe that C++ has always been a common practice for either member variables or constructor arguments to be marked prefixes or suffixes.For example, a rule that ends a member variable with a "_".

2022-09-30 14:10

If you have any answers or tips

© 2023 OneMinuteCode. All rights reserved.