Skip to content

Commit e91092f

Browse files
authored
Merge pull request #93 from php-java/fix/exception-handling
Handle the previous exception in UnableToCatchException
2 parents 7cbdf39 + b5f6b2f commit e91092f

4 files changed

Lines changed: 32 additions & 4 deletions

File tree

src/Kernel/Mnemonics/_invokestatic.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ public function execute(): void
8989
}
9090

9191
throw new UnableToCatchException(
92-
$expectedClass . ': ' . $e->getMessage()
92+
$expectedClass . ': ' . $e->getMessage(),
93+
0,
94+
$e
9395
);
9496
}
95-
97+
9698
if ($signature[0]['type'] !== 'void') {
9799
$this->pushToOperandStack($return);
98100
}

src/Kernel/Mnemonics/_invokevirtual.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ public function execute(): void
8282
}
8383

8484
throw new UnableToCatchException(
85-
$expectedClass . ': ' . $e->getMessage()
85+
$expectedClass . ': ' . $e->getMessage(),
86+
0,
87+
$e
8688
);
8789
}
8890

tests/TryCatchTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace PHPJava\Tests;
33

4+
use PHPJava\Exceptions\UnableToCatchException;
5+
use PHPJava\Packages\java\lang\IndexOutOfBoundsException;
46
use PHPUnit\Framework\TestCase;
57

68
class TryCatchTest extends Base
@@ -37,7 +39,7 @@ public function testPassthroughCatchStatement()
3739
);
3840
}
3941

40-
public function testImitationThroExceptionw()
42+
public function testImitationThrowException()
4143
{
4244
$result = $this->initiatedJavaClasses['TryCatchTest']
4345
->getInvoker()
@@ -50,4 +52,21 @@ public function testImitationThroExceptionw()
5052
$result
5153
);
5254
}
55+
56+
public function testImitationThrownExceptionHasPreviousException()
57+
{
58+
$this->expectException(IndexOutOfBoundsException::class);
59+
$this->expectExceptionMessage('String index out of range: -1');
60+
61+
try {
62+
$result = $this->initiatedJavaClasses['TryCatchTest']
63+
->getInvoker()
64+
->getStatic()
65+
->getMethods()
66+
->call('testImitationThrownExceptionHasPreviousException');
67+
} catch (UnableToCatchException $e) {
68+
// Unwrap the original exception
69+
throw $e->getPrevious();
70+
}
71+
}
5372
}

tests/fixtures/java/TryCatchTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ public static int testImitationThrowException()
3333

3434
return 1;
3535
}
36+
37+
public static void testImitationThrownExceptionHasPreviousException()
38+
{
39+
"".charAt(-1);
40+
}
3641
}

0 commit comments

Comments
 (0)