_mabrar_'s avatar

Need Suggestion about Try Catch

What should i use in try catch in catch section Throwable or Exception:

  /**
     * JWt Authorization
     *
     * @return JsonResponse
     */
    public function jwtAuthorization(): JsonResponse
    {
        $response = ['result' => [], 'errors' => []];
        $httpStatusCode = Response::HTTP_OK;
        try {
            $_TOKEN = $this->genJwt(\request());
            $jws = $this->serializer->unserialize($_TOKEN);
            $isAuthentic = $this->jwtTokenService->verifyAuthenticity($jws);
            $isNotExpired = $this->jwtTokenService->verifyExpiration($jws);
            $response['result']['isAuthentic'] = $isAuthentic;
            $response['result']['isNotExpired'] = $isNotExpired;
        } catch (\Throwable $e) {
            $httpStatusCode = Response::HTTP_BAD_REQUEST;
            $response['errors'][] = $e->getTrace();
        }
        return \response()->json($response, $httpStatusCode);
0 likes
3 replies
_mabrar_'s avatar

@tykus Exactly, but in most of the documentations Throwable is not preferable as much Exception.

tykus's avatar

@mabrar If you want to catch and handle a specific Exception, then that is preferred obviously.

Please or to participate in this conversation.