function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kailash chandrakailash chandra 

How to get 100% code coverage of an apex using try-catch block

I am unable to cover the catch block of this code.
    public static ID getUserId() {
           return userinfo.getuserid()            
        catch (Exception ex) {
            return ex.getMessage();
User-added image

Ajay K DubediAjay K Dubedi
Hi Kailash,

I have gone through your code. I do not think that you can cover that catch block because to a cover catch block one needs to through an exception. 
But your code contains just one line which is a standard method call and does not perform any calculation.
I would recommend removing the try-catch block from such a small class.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi