How can the setExpectedException method in PHPUnit be used effectively to test for specific exceptions in PHP code?

When testing PHP code with PHPUnit, the setExpectedException method can be used to specify which exception is expected to be thrown during a test. This is useful for ensuring that the code behaves as expected in error conditions. By using setExpectedException with the specific exception class, the test will pass only if that exception is thrown, providing a targeted way to test exception handling in the code.

public function testSpecificExceptionIsThrown()
{
    $this->expectException(YourSpecificException::class);

    // Code that should throw YourSpecificException
}