我有一个自定义异常,我想在一个函数中抛出并在另一个函数中捕获,它不起作用

I have a custom exception I want to throw in one function and catch in another, it isn't working

所以我有一个自定义异常 class "ExceptionLinkedAccess".

我在另一个 class 中有一个函数 "find" 会抛出这个异常。我在与 "find" 相同的 class 中有另一个名为 "insert" 的函数,该函数将调用 "find"。

我需要捕获 find 抛出的异常。所以,我使用了 try/catch 块,但它不起作用,因为程序在抛出异常时才结束。

很遗憾,我无法更改任何函数签名或声明。

template <typename T>
class OULinkedList {
    template <typename F>
    friend class OULinkedListEnumerator;
private:
    Comparator<T>* comparator = NULL;               // used to determine list order and item equality
    unsigned long size = 0;                         // actual number of items currently in list
    OULink<T>* first = NULL;                        // pointer to first link in list
    OULink<T>* last = NULL;                         // pointer to last link in list
public:
    OULinkedList(Comparator<T>* comparator);        // creates empty linked list with comparator
    virtual ~OULinkedList();                        // deletes all links and their data items
    void DisplayList();                     

    // if an equivalent item is not already present, insert item in order and return true
    // if an equivalent item is already present, leave list unchanged and return false
    bool insert(const T* item);

    // if item is greater than item at last, append item at end and return true
    // if item is less than or equal to item at last, leave list unchanged and return false
    bool append(const T* item);

    // if an equivalent item is already present, replace item and return true
    // if an equivalent item is not already present, leave list unchanged and return false
    bool replace(T* item);

    // if an equivalent item is already present, remove item and return true
    // if an equivalent item is not already present, leave list unchanged and return false
    bool remove(T* item);

    // if any items are present, return a copy of the first item
    // if no items are present, throw new ExceptionLinkedListAccess
    T get() const;

    // if an equivalent item is present, return a copy of the first such item
    // if an equivalent item is not present, throw a new ExceptionLinkedListAccess
    T find(const T* item) const;

    unsigned long getSize() const;                  // returns the current number of items in the list

    OULinkedListEnumerator<T> enumerator() const;   // create an enumerator for this linked list
};

// Implementation goes here
template<typename T>
 OULinkedList<T>::~OULinkedList(){
    delete comparator;
    delete first;
    delete last;

    if(comparator != NULL){
        comparator = NULL;
    }
    if(first != NULL){
        first = NULL;
    }
    if(last != NULL){
        last = NULL;
    }

 }


template<typename T>
OULinkedList<T>::OULinkedList(Comparator<T>* comparator){
    this->comparator = comparator;
}

// if an equivalent item is not already present, insert item in order and return true
// if an equivalent item is already present, leave list unchanged and return false
template <typename T>
bool OULinkedList<T>::insert(const T* item){
    //check if exist

    try{

        find(item);

    }catch(ExceptionLinkedListAccess ex){

        if(!append(item)){

        std::cout << "INSERTING" << std::endl;
        }
    }

}


// if item is greater than item at last, append item at end and return true
// if item is less than or equal to item at last, leave list unchanged and return false
template <typename T>
bool OULinkedList<T>::append(const T* item) {



    if(this->first == NULL){

        this->first = new OULink<T>(item);

        this->last = first;
        size++;

        return true;

    }else if(comparator->compare(*item, *this->last->data) > 0){ 

        this->last->next = new OULink<T>(item);
        this->last = this->last->next;
        // delete if not working
        this->last->next = NULL;

        size++;
        return true;

    }else{

        return false;

    }

}


template <typename T>
bool OULinkedList<T>::replace(T* item){
    return false;

}


// if an equivalent item is already present, remove item and return true
// if an equivalent item is not already present, leave list unchanged and return false
template <typename T>
bool OULinkedList<T>::remove(T* item){
    OULinkedListEnumerator<T> listEnum(this->first);
    // Special case, remove head
    if(listEnum->current == NULL && this->first){
        OULink<T>* successorNode = this->first->next;
        if(successorNode == NULL){
            this->last = NULL;
        }
}

    return true;
}


// if any items are present, return a copy of the first item
// if no items are present, throw new ExceptionLinkedListAccess
template <typename T>
T OULinkedList<T>::get() const{
    OULinkedListEnumerator<T> linkE(this->first);
    return linkE.peek();

}


// if an equivalent item is present, return a copy of the first such item
// if an equivalent item is not present, throw a new ExceptionLinkedListAccess
template <typename T>
T OULinkedList<T>::find(const T* item) const {
    OULink<T>* currentNode = this->first;

    while(currentNode != NULL){
        if(comparator->compare(*currentNode->data,*item) == 0){
            return *currentNode->data;
        }
        currentNode = currentNode->next;
    }
    throw  new ExceptionLinkedListAccess;
}

这是我的 Exception.h:

class Exception {};
class ExceptionIndexOutOfRange : Exception {};
class ExceptionMemoryNotAvailable : Exception {};
class ExceptionLinkedListAccess : Exception {};
class ExceptionEnumerationBeyondEnd : Exception {};

正如 Yksisarvinen 所指出的,new 关键字在 java 和 c++ 中的工作方式不同。

我删除了新的,现在我可以捕获异常了。