Skip to content

Commit f98dfa5

Browse files
committed
Handle the previous exception in UnableToCatchException
1 parent 8fb83f5 commit f98dfa5

4 files changed

Lines changed: 35 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: 23 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,24 @@ public function testImitationThroExceptionw()
5052
$result
5153
);
5254
}
55+
56+
public function testImitationThrownExceptionHasPreviousException()
57+
{
58+
try {
59+
$result = $this->initiatedJavaClasses['TryCatchTest']
60+
->getInvoker()
61+
->getStatic()
62+
->getMethods()
63+
->call('testImitationThrownExceptionHasPreviousException');
64+
} catch (UnableToCatchException $e) {
65+
$this->assertInstanceOf(
66+
IndexOutOfBoundsException::class,
67+
$e->getPrevious()
68+
);
69+
$this->assertEquals(
70+
'String index out of range: -1',
71+
$e->getPrevious()->getMessage()
72+
);
73+
}
74+
}
5375
}

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)