source: trunk/abcl/examples/java-exception/lispfunctions.lisp

Last change on this file was 11385, checked in by vvoutilainen, 16 years ago

Add a simple example of catching a java exception in lisp code.

  • Property svn:keywords set to Id
File size: 1.4 KB
Line 
1;;; lispfunctions.lisp
2;;;
3;;; Copyright (C) 2008 Ville Voutilainen
4;;; $Id: lispfunctions.lisp 11385 2008-11-08 19:06:53Z vvoutilainen $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20; we need to get the
21; 1) class (Main)
22; 2) classes of the parameters (int)
23; 3) method reference (getting that requires the class
24; of our object and the classes of the parameters
25
26; After that we can invoke the function with jcall,
27; giving the method reference, the object and the parameters.
28; The function throws an exception, so we wrap the call in
29; handler-case.
30(defun void-function (param)
31  (let* ((class (jclass "Main"))
32   (intclass (jclass "int"))
33   (method (jmethod class "addTwoNumbers" intclass intclass)))
34    (handler-case
35  (jcall method param 2 4)
36      (java-exception (exception)
37  (format t "Caught a java exception in void-function~%")))))
38
Note: See TracBrowser for help on using the repository browser.