-4

I'm writing a simple program to search for an element in a 2D matrix. It's not giving me an active/named exception, but it just throws me a Runtime error.

Any help with as to why this is happening?

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        for (int i=0 ;i< matrix.size(); ++i){
            int m = matrix[i].size() -1 ;
            int n = matrix.size()-1;
            if (matrix[n][m] < target){
                return false;
            }
            else if (matrix[i][m] < target){
                i++;
            } else {
                 for (int j=0;j <= m; j++ ){
                    if (matrix[i][j] == target){
                        return true;
                    } else if (matrix[i][j] != target) {
                        continue;
                    }
                }
                
                 if (matrix[i][m] != target ){
                    return false;
                }
            }      
        }throw; 
    }
};

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 4
    What is `throw;` supposed to do? Don't use such sites to learn *anything*, that's not their purpose whatever you might have heard. If you want to learn C++ invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). If you want to learn programming in general, take computer-science classes and learn multiple languages. – Some programmer dude Jul 25 '22 at 09:26
  • `throw` is executed immediately after reaching exit from block it precedes ( slight oversight on cppreference's side not to mention it) . The message is just result of `throw` without arguments, it's meant to re-throw active exception (in handler) – Swift - Friday Pie Jul 25 '22 at 09:36

1 Answers1

2
    }throw; 

A throw without following expression can be used inside an exception handler to re-throw the currently handled exception.

However, at this point your program does not have an active exception. In this case, throw calls std::terminate().

This seems to be an edit artifact.

DevSolar
  • 67,862
  • 21
  • 134
  • 209