mirror of
https://github.com/php/php-src.git
synced 2025-08-19 08:49:28 +02:00
Add experimental APIs to get and clear the last exception
This commit is contained in:
parent
fd3d84ee52
commit
ee36612cef
8 changed files with 150 additions and 2 deletions
|
@ -20,7 +20,15 @@ What is PHP4 ext/java?
|
|||
the same syntax. Furthermore, if the java object is of type
|
||||
"java.lang.Class", then static members
|
||||
|
||||
4) Exceptions raised result in PHP warnings, and null results.
|
||||
4) Exceptions raised result in PHP warnings, and null results. The
|
||||
warnings may be eliminated by prefixing the method call with an
|
||||
"@" sign. The following experimental APIs may be used to retrieve
|
||||
and reset the last error:
|
||||
|
||||
java_last_exception_get()
|
||||
java_last_exception_clear()
|
||||
|
||||
These APIs are not currently implemented in a reentrant fashion.
|
||||
|
||||
5) Overload resolution is in general a hard problem given the
|
||||
differences in types between the two languages. The PHP Java
|
||||
|
|
23
ext/rpc/java/except.php
Normal file
23
ext/rpc/java/except.php
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?
|
||||
$stack=new Java("java.util.Stack");
|
||||
$stack->push(1);
|
||||
|
||||
#
|
||||
# Should succeed and print out "1"
|
||||
#
|
||||
$result = $stack->pop();
|
||||
$ex = java_last_exception_get();
|
||||
if (!$ex) print "$result\n";
|
||||
|
||||
#
|
||||
# Should fail - note the "@" eliminates the warning
|
||||
#
|
||||
$result=@$stack->pop();
|
||||
$ex=java_last_exception_get();
|
||||
if ($ex) print $ex->toString();
|
||||
|
||||
#
|
||||
# Reset last exception
|
||||
#
|
||||
java_last_exception_clear();
|
||||
?>
|
|
@ -384,6 +384,36 @@ void java_call_function_handler
|
|||
|
||||
/***************************************************************************/
|
||||
|
||||
PHP_FUNCTION(java_last_exception_get)
|
||||
{
|
||||
jlong result = 0;
|
||||
jmethodID lastEx;
|
||||
|
||||
(pval*)(long)result = return_value;
|
||||
|
||||
lastEx = (*jenv)->GetStaticMethodID(jenv, php_reflect, "lastException",
|
||||
"(J)V");
|
||||
|
||||
(*jenv)->CallStaticVoidMethod(jenv, php_reflect, lastEx, result);
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
PHP_FUNCTION(java_last_exception_clear)
|
||||
{
|
||||
jlong result = 0;
|
||||
jmethodID clearEx;
|
||||
|
||||
(pval*)(long)result = return_value;
|
||||
|
||||
clearEx = (*jenv)->GetStaticMethodID(jenv, php_reflect, "clearException",
|
||||
"()V");
|
||||
|
||||
(*jenv)->CallStaticVoidMethod(jenv, php_reflect, clearEx);
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static pval _java_getset_property
|
||||
(zend_property_reference *property_reference, jobjectArray value)
|
||||
{
|
||||
|
@ -475,6 +505,8 @@ PHP_MSHUTDOWN_FUNCTION(java) {
|
|||
}
|
||||
|
||||
function_entry java_functions[] = {
|
||||
PHP_FE(java_last_exception_get, NULL)
|
||||
PHP_FE(java_last_exception_clear, NULL)
|
||||
{NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
|
|
|
@ -88,12 +88,23 @@ public class reflect {
|
|||
}
|
||||
}
|
||||
|
||||
static Throwable lastException = null;
|
||||
|
||||
static void lastException(long result) {
|
||||
setResult(result, lastException);
|
||||
}
|
||||
|
||||
static void clearException() {
|
||||
lastException = null;
|
||||
}
|
||||
|
||||
static void setException(long result, Throwable e) {
|
||||
if (e instanceof InvocationTargetException) {
|
||||
Throwable t = ((InvocationTargetException)e).getTargetException();
|
||||
if (t!=null) e=t;
|
||||
}
|
||||
|
||||
lastException = e;
|
||||
setException(result, e.toString().getBytes());
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue