Implement funcionality with handling errrors without throwing exceptions In usual code if something goes wrong you throw an exception. However in some cases you need so that exceptions were processed and reported. Form validation is a good example. Error handling in this is realy easy. However I faced a problem when error handling is different from the previous example and that code does not look very pretty. Here is an example:

public function create($bcId) {
        $this-&gt;reporter-&gt;title("BC product resource creating report");
        $resource = $this-&gt;get($bcId);
        if (!$resource) {
            $this-&gt;reporter-&gt;report();
            return;
        }
        $dto = EntityDto::fromBc($resource);
        if (!$this-&gt;validator-&gt;validate($dto)) {
            $this-&gt;reporter-&gt;report($this-&gt;validator-&gt;getErrors());
            return;
        }
        $entity = Entity::fromDto($dto);
        $this-&gt;entityRepo-&gt;create($entity);
    }

    public function get($bcId) {
        try {
            $resource = $this-&gt;bcRepo-&gt;get($bcId);
        } catch (\Exception $e) {
            $this-&gt;reporter-&gt;add($e);
            return false;
        }
        return $resource;
    }


Without error handling the code would look like this:

public function create() {
        $resource = $this-&gt;bcRepo-&gt;get($bcId);
        $dto = EntityDto::fromBc($resource);
        $entity = Entity::fromDto($dto);
        $this-&gt;entityRepo-&gt;create($entity);
    }


Much less code and more clear. However I need proper error handling. I can not go with throwing exceptions because it will exit the app. I could have the following code:

public function create() {
        if (
            ($resource = $this-&gt;bcRepo-&gt;get($bcId)) &amp;&amp;
            ($dto = EntityDto::fromBc($resource)) &amp;&amp;
            ($entity = Entity::fromDto($dto)) &amp;&amp;
            ($this-&gt;entityRepo-&gt;create($entity))
        ) ; else {
            $this-&gt;report();
        }
    }


But it still is difficult to understand. Are there any variants to change the code to by prettier without chaning its behaviour?
I can not go with throwing exceptions because it will exit the app.


That's but a grave delusion and misconception about exception. What you have to learn yet, there is an infinite number of different exceptions, and catch (\Exception $e) is not the only way to catch an exception. 

So you have a solution in three easy steps

First, define your own kind of exception:

class MyFormHandlerErrorException extends Exception {}


then make your methods throw this certain exception

if (something goes wrong) {
    throw new \MyFormHandlerErrorException('Error message goes here');
}


and then simply catch this only one

public function create() {
    try{
        $resource = $this-&gt;bcRepo-&gt;get($bcId);
        $dto = EntityDto::fromBc($resource);
        $entity = Entity::fromDto($dto);
        $this-&gt;entityRepo-&gt;create($entity);
    } catch (\MyFormHandlerErrorException  $e) {
        $this-&gt;reporter-&gt;add($e);
        return false;
    }
}


bingo! now your specific exceptions will be caught whereas all other error will bubble up to error handler. As simple as that

If you want to define several different exceptions of the same kind, then just create one basic exception and then extend all others from this:

class MyFormHandlerErrorExceptionA extends MyFormHandlerErrorException {}
class MyFormHandlerErrorExceptionB extends MyFormHandlerErrorException {}    
class MyFormHandlerErrorExceptionN extends MyFormHandlerErrorException {}


now you could either catch any of them specifically, or catch them all at once by catching the base exception