Program stops after exception handling

Asked 2 years ago, Updated 2 years ago, 37 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

List.h

#ifndef LAB9_LIST_H
#define LAB9_LIST_H

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

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

};

template<typename T>
class Link
{
private:
    T value;
    Link*next;
public:
    // constructor
    Link(T value, Link*next=nullptr): value(value), next(next){}
    // destructor
    virtual~Link(){}
    // 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
{
private:
    Link <T>*head;


public:
    List():head(nullptr){}


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

main.cpp

#include<iostream>
# include "List.h"

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


int main()
{

    List <int>intList;

    // testing exception
    try{
        cout<<intList.getHead()<endl;
        cout<<"Should have had an exception."<endl;
    }
    catch(list_error&ex){
        cout<<"List error saying"<<ex.what()<<endl;
    }
    catch(...){
        cout<<"Cough something else"<<endl;
    }

    return 0;
}

c++

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


© 2024 OneMinuteCode. All rights reserved.