Home arrow PHP arrow Page 6 - Using PHP with Java

An Exceptionally Clever Cow - PHP

You probably already know that PHP comes with extensions for awide variety of different technologies. But did you know that you couldhook PHP up to Java, and use Java classes and Java Beans from withinyour PHP scripts? If this is news to you, keep reading.

TABLE OF CONTENTS:
  1. Using PHP with Java
  2. Getting Started
  3. Rank And File
  4. A Custom Job
  5. Passing The Parcel
  6. An Exceptionally Clever Cow
  7. Beanie Baby
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 17
April 03, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Next, let's look at exception handling. If you go back to the example on the previous page, you'll see that the Java class includes a couple of exception handlers, one for IOExceptions and one for everything else. Let's now modify the PHP script so that it includes some basic exception handling.

<?php // create an instance of the ReverseString object $obj = new Java("ReverseString"); // create an instance of the StringWriter object $writer = new Java("java.io.StringWriter"); $obj->setWriter($writer); $obj->reverse("The cow jumped over the moon"); // get the last exception $e = java_last_exception_get(); if ($e) { // print error echo $e->toString(); } else { echo $writer->toString(); $writer->flush(); $writer->close(); } // clear the exception java_last_exception_clear(); ?>
This version of the script uses two built-in PHP functions to check whether or not any exceptions were raised during the execution of the class methods. If any exceptions were raised, they would be stored in the $e PHP variable, and printed via a call to the toString() method.

If you'd like to see how an exception is handled, modify the PHP script above to look like this:

<?php // create an instance of the ReverseString object $obj = new Java("ReverseString"); // create an instance of the StringWriter object $writer = new Java("java.io.StringWriter"); // deliberately introduce an error by commenting out the next line // $obj->setWriter($writer); // suppress errors here @$obj->reverse("The cow jumped over the moon"); // get the last exception $e = java_last_exception_get(); if ($e) { // print error echo $e->toString(); } else { echo $writer->toString(); $writer->flush(); $writer->close(); } // clear the exception java_last_exception_clear(); ?>
Here's what the output looks like:

java.lang.Exception: Something really bad happened
Of course, this simple and graceful error message gets a little more unfriendly if you remove the @ error-suppression operator.

Warning: java.lang.Exception: Something really bad happened in /usr/local/apache/htdocs/java/exception.php on line 10 java.lang.Exception: Something really bad happened


 
 
>>> More PHP Articles          >>> More By Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: