[racket] tcp-read handlers

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Sat Jan 18 08:04:07 EST 2014

Eduardo Bellani wrote at 01/18/2014 06:50 AM:
> Is there a way to install a handler for the tcp-error exception?

Unfortunately, raised exceptions error messages aren't displayed with 
the exception struct name, and thrown exceptions aren't documented and 
statically checked like they are in Java, so it's sometimes hard to 
figure out the exception predicate to use.  But you can see the struct 
name for an exception by triggering and catching the exception in an 
experiment:

#lang racket

(require net/url)

(with-handlers ((exn:fail? (lambda (e) e)))
   (get-pure-port (string->url "http://nonexistent.example.com/")))

;;=> #(struct:exn:fail:network:errno
;;     "tcp-connect: connection failed\n  detail: host not found\n  
address: nonexistent.example.com\n  port number: 80\n  step: 1\n  system 
error: Name or service not known; errno=-2"
;;     #<continuation-mark-set>
;;     (-2 . gai))

This experiment suggests that the predicate "exn:fail:network:errno?" 
would work for catching at least some "tcp-connect" exceptions.  You can 
do a similar experiment to find the predicate you should use for your 
"tcp-error" exception.

Neil V.


Posted on the users mailing list.