[DependencyInjection] Replaced try/catch block with an @expectedException annotation in a test.