Search code examples
schemechicken-scheme

Error handling in Chicken Scheme


I'm making a basic port scanner. I'm trying to print "closed" when I connect to a closed port and "open" when I connect to an open port.

Right now I'm doing this:

(condition-case (tcp-connect (list-ref host 0) port)
    [(exn) (print "closed")]
    ['nil (print "open")]))

My open isn't printing correctly when it should (have tested with netcat). How I understand condition-case is it matches errors. I'm trying to handle the case of no errors i.e. connection is successful.

Is there a better approach to this? I feel that I'm over thinking it.


Solution

  • Not being familiar with Chicken I would expect something like this to be what you want:

    (condition-case 
        (begin (tcp-connect (list-ref host 0) port)
               (print "open"))
        [(exn) (print "closed")])
    

    That is, the expression given to condition-case is the "happy path" (like a try block in Java).