0

Using curses module for python I know that writing in the lower-right corner raise an error (something to do with the cursor having no "next place" to go to).

But I do not care; all I wanted is to write a character when possible otherwise no big deal. So I though of using try-except-pass construct

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import curses

def doStuff(stdscr):
    Y , X = stdscr.getmaxyx()       # get screen size

    try:
        stdscr.addch(Y-1,X-1,'X')   # try to print in forbiden corner
    except:
        pass                        # do nothing when error is raised

    stdscr.refresh()
    stdscr.getch()

curses.wrapper(doStuff)

I thougth that it would just "pass" and ignore the "addch" instruction. But to my surprise the character is printed!

Could someone explain me the reason?

ps: I confirm that without the try-except construct I get :

_curses.error: addch() returned ERR

mr caca
  • 15
  • 4

1 Answers1

0

Try catch does not ignores method. It executes it in normal flow, and IF exception is raised it handles it. Consider following:

def raising_method():
    print "a"
    print "b"
    raise Exception

try:
    raising_method()
except Exception as e:
    pass

"a" and "b" would still be printed.

May it be your problem? I don't know curses module, so can't tell you anything module-specific.

Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93
  • I see... So it is not really curses related. I thought that try-except behaved like some kind of if-else loop so that if an error is raised, only the instructions in the "except" block would be executed. Thank you for your answer. – mr caca Oct 03 '14 at 08:58
  • To make this curses-specific, the confusion here is because curses raises that exception only after already printing the character in the bottom-right. The try block terminates as soon as the exception is raised, and the except-pass block then runs (and does nothing). But curses doesn't undo the fact that it printed something to your terminal because an exception was caught *later*. – Soren Bjornstad Oct 20 '19 at 02:26