s c h e m a t i c s : c o o k b o o k

/ WebHome / CookbookBrainStorm / Cookbook.GettingStartedExceptions

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Exceptions

The exception system in MzScheme is the one proposed by Friedman, Haynes and Dybvig.

A program can signal an exceptional situation by raising an expression. The form (raise exn) will pass exn, which can be anything, to the current exception handler. The form with-handlers can be used to temporarily extend the current exception handler while executing its body of code.

> (define (always-true exn) #t)
> (define (my-handler exn)
    (for-each display
              (list "Exception caught: " exn "\n")))
> (with-handlers ([always-true my-handler])
    (/ 1 0))
Exception caught: #<struct:exn:application:divide-by-zero>

The above example used the predicate always-true to determine whether the exception handler my-handler should take care of the exception or whether it should be passed on to the current exception handler. Since always-true always returns #t all exceptions will be handled by my-handler.

Since the stop button in DrScheme works by raising a break exception, normally one uses the predicate exn:fail? in order to catch all exceptions but breaks. If exn:fail? is used, the stop button works even if a programming mistake is made in the exception handler.

; This loops indefinitely until the stop button is pressed
> (with-handlers ([exn:fail? (let loop () (loop))])
    (/ 1 0))
[bug icon] user break
> 

It is good style to only catch the exceptions that one is interested in. Therefore check the HelpDesk to see, what exceptions are thrown by the various functions.

As an example lets look at tcp-connect. The HelpDesk says:

If a connection cannot be established by tcp-connect, the exn:fail:network exception is raised.

Therefore we need to catch the exn:fail:network exception:

; There is no service running on port 83 on scheme.dk,
; so tcp-connect will raise an exception after a little while.
> (with-handlers ([exn:fail:network? my-handler])
    (tcp-connect "www.scheme.dk" 83))
Exception caught: #<struct:exn:fail:network>

Notice that tcp-connect blocks breaks. If you want to enable breaks during tcp-connects use the function tcp-connect/enable-break instead.

-- JensAxelSoegaard - 26 Apr 2004

The following snippet shows how we can create application-level exceptions that subtype exn:fail.

(define-struct (exn:fail:you-silly-head exn:fail) ())

(define (raise-you-silly-head)
  (raise (make-exn:fail:you-silly-head
          "you silly head"
          (current-continuation-marks))))

Comments

not-break-exn? is obsolete as of one of the PLT v299 revisions. I believe it has been replaced by exn:fail?.

-- AntonVanStraaten - 11 Oct 2005

CookbookForm
TopicType: Recipe
ParentTopic: GettingStartedRecipes
TopicOrder:

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest