0

I am working on an othello game for class and am trying to raise an exception when the player makes a valid move but this exception is making my program crash and im not sure why. I get the error:

Traceback (most recent call last): File "C:\Users\chapp_000\Desktop\Othello\OthelloUI.py", line 46, in <module> s.makeMove(move[0],move[-1]) File "C:\Users\chapp_000\Desktop\Othello\OthelloGL.py", line 73, in makeMove raise InvalidMoveError() OthelloGL.InvalidMoveError 

Here is my exception class:

class InvalidMoveError(Exception): '''for handling bad moves''' pass 

And here is where the exception is being raised:

if self.checkCell(r,c) == 0: if self.turn ==1: self.board[r][c] = 2 self.turn =2 else: self.board[r][c] = 1 self.turn =1 elif self.checkCell(r,c) !=0: raise InvalidMoveError() 

EDIT: to answer some comments I am tying to us my exception to stop my program from crashing by saying pass and check cell looks on my board and returns 0 if no piece is their and 1 or 2 if that player has a piece their line 46 of the code is s.makeMove(move[0],move[-1]) where s is a game bored object and move is a list of 2 numbers from the player saying the x and y location they would like to move to

7
  • 2
    Could you point out line 46 and 73 Commented May 27, 2015 at 8:35
  • Mention the details of all the functions you're using such as checkCell Commented May 27, 2015 at 8:35
  • 4
    This is the expected behavior when raising an exception. What do you actually want to do? Commented May 27, 2015 at 8:36
  • line 73 is my raise InvalidMoveError() line and line 46 is where I call the function it's in (not posted because I didn't think the call was a relevant piece of code to the problem and I didn't want to make people read to much code I will edit in more details Commented May 27, 2015 at 8:37
  • Uncaught exceptions halt programs. Yes, they do. Commented May 27, 2015 at 8:40

1 Answer 1

3

You need to call your method like this if you want to catch the exception and do something with it:

try: s.makeMove(move[0], move[-1]) except InvalidMoveError: print('You made an invalid move') # do something else instead 

Otherwise, exceptions that are not handled will cause your program to terminate. That’s the point.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you :) I was a bit confused on the concept of when to call try and catch because im writing my program in separate modules and classes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.