How properly handle error exception in try and catch block?

I have a problem with the flow of error handling. I have a try and catch block, and I need to ensure that the try block contains the logic for what should be executed. Specifically, a ticket should be created. However, the creation of the ticket can fail for two reasons. The first reason is that it cannot find the responder_id, which means either it doesn’t exist or it is working with a responder_id that is no longer in the database. In this case, I need to create a new ticket but set the responder_id to null, which I have implemented and it works.

My issue is with throwing errors. I need to ensure that the try-catch block can throw two types of errors. The first one should be thrown if the failure is due to an invalid responder_id, and the second one should be a general error if the ticket creation fails for any other reason. When I throw an exception only in the catch block, the exception is not thrown even though it is clearly dealing with an invalid responder_id.

I’ve gotten a bit lost and can’t figure out how to do it correctly. Could anyone please offer some advice?

This is code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if ($agent = $this->getAgent()) {
$data["responder_id"] = $agent;
}
try {
$response = FreshdeskService::createTicket($data, $this->getTo(), $attachments);
$success = $response["success"] ?? false;
if (!$success) {
$error_response = json_decode($response['response'], true);
$error_details = $error_response['errors'] ?? [];
foreach ($error_details as $error) {
if ($error['field'] === 'responder_id') {
}
}
throw new Exception("Create ticket failed due to invalid responder_id: " . $this->getAgent() . ". Error details: " . json_encode($error_details));
}
} catch (Exception $e) {
$data['responder_id'] = null;
$response = FreshdeskService::createTicket($data, $this->getTo(), $attachments);
Log::info($response);
$success = $response["success"] ?? false;
if (!$success) {
throw new Exception("Retry ticket creation failed: From: " . $this->getFrom() . ", To: " . $this->getTo() . ", Subject: " . $this->subject . ", Response: " . json_encode($response["response"] ?? []) . ", Data: " . json_encode($data));
}
throw new Exception($e->getMessage());
}
</code>
<code>if ($agent = $this->getAgent()) { $data["responder_id"] = $agent; } try { $response = FreshdeskService::createTicket($data, $this->getTo(), $attachments); $success = $response["success"] ?? false; if (!$success) { $error_response = json_decode($response['response'], true); $error_details = $error_response['errors'] ?? []; foreach ($error_details as $error) { if ($error['field'] === 'responder_id') { } } throw new Exception("Create ticket failed due to invalid responder_id: " . $this->getAgent() . ". Error details: " . json_encode($error_details)); } } catch (Exception $e) { $data['responder_id'] = null; $response = FreshdeskService::createTicket($data, $this->getTo(), $attachments); Log::info($response); $success = $response["success"] ?? false; if (!$success) { throw new Exception("Retry ticket creation failed: From: " . $this->getFrom() . ", To: " . $this->getTo() . ", Subject: " . $this->subject . ", Response: " . json_encode($response["response"] ?? []) . ", Data: " . json_encode($data)); } throw new Exception($e->getMessage()); } </code>
if ($agent = $this->getAgent()) {
                $data["responder_id"] = $agent;
            }

            try {
                $response = FreshdeskService::createTicket($data, $this->getTo(), $attachments);
                $success = $response["success"] ?? false;

                if (!$success) {
                    $error_response = json_decode($response['response'], true);
                    $error_details = $error_response['errors'] ?? [];

                    foreach ($error_details as $error) {
                        if ($error['field'] === 'responder_id') {
                        }
                    }
                    throw new Exception("Create ticket failed due to invalid responder_id: " . $this->getAgent() . ". Error details: " . json_encode($error_details));

                }
            } catch (Exception $e) {
                $data['responder_id'] = null;
                $response = FreshdeskService::createTicket($data, $this->getTo(), $attachments);
                Log::info($response);
                $success = $response["success"] ?? false;

                if (!$success) {
                    throw new Exception("Retry ticket creation failed: From: " . $this->getFrom() . ", To: " . $this->getTo() . ", Subject: " . $this->subject . ", Response: " . json_encode($response["response"] ?? []) . ", Data: " . json_encode($data));
                }
                throw new Exception($e->getMessage());


            }

I hope this post will find someone who can give me some advice about this problem.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật