-1

Here is my code:

from SimPy.Simulation import *

class Message(Process):
    def arrive(self, destination):
        yield hold, self, 2
        try:
            print "%s %s going to %s" % (now(), self.name, destination.name)
            self.interrupt(destination)
        except NameError, x:
            print "%s is out of reach" % x

What I want to do is to print out that destination is unreachable when its name doesn't exist, but I'm still getting usual python error:

Traceback (most recent call last):
  File "<pyshell#22>", line 1, in <module>
    message.arrive(node2)
NameError: name 'node2' is not defined
Mureinik
  • 297,002
  • 52
  • 306
  • 350
tobi
  • 75
  • 1
  • 7

1 Answers1

0

Your name error does not occur in the method. It occurs before the method is called.

Python tries to resolve node2 before it can pass the value of node2 to the message.arrive() method. The method code is never executed.

You'd get the same error if you just typed node2 in your shell, you did not define it so Python doesn't know how to use it's value then either.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Hmm, guess you are wright. Any idea how to throw an exception in that case? – tobi Mar 24 '13 at 12:33
  • @tobi: it *already* is throwing an exception in that case, a `NameError`. If you meant how to catch it: Write the `try:` `except` in the shell instead. – Martijn Pieters Mar 24 '13 at 12:35