-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
4
  • Where are you calling message.arrive(node2)? Mar 24, 2013 at 12:29
  • i just wrote it in shell
    – tobi
    Mar 24, 2013 at 12:31
  • Then you didn't define node2. Mar 24, 2013 at 12:32
  • Yes i didnt. I want it to catch an exception if some node is trying to reach a node that desn't exist
    – tobi
    Mar 24, 2013 at 12:36

1 Answer 1

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.

2
  • Hmm, guess you are wright. Any idea how to throw an exception in that case?
    – tobi
    Mar 24, 2013 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, 2013 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.