Is there a way to throw custom error if PDA account does not exist in a Solana program?

1 Answer 1


To check if a PDA is initialized, or check it against an account being passed into the program, the following can be done (if using anchor)

Import Pubkey:

use anchor_lang::solana_program::pubkey::Pubkey;

Use find_program_address to derive the PDA:

let (pda, bump) = Pubkey::find_program_address(
    &[b"some_seed", &*seed.key().as_ref()], &self::ID //programId

Check it against an account passed into the instruction:

 if pda != some_account.key()  {
    return Err(ProgramError::Custom(1))

If the the PDA is not initialized, the instruction will throw "0xbc4".

EDIT: If you want to create the PDA within the same instruction, add the following to your #[derive(Accounts)]:

     space = <[calculate how much space the acct needs][1]>,
     payer = payer,
     seeds = [
 pub pda_account: Account<'info, <some account struct>,

Be sure to use seeds that ensure no collision if this account will be derived multiple times. Also, if you want run the check if it exists, then derive, you can split these into multiple instructions.

  • and How to derive account from this pda using anchor framework?
    – blackhorse
    Commented Jan 12 at 14:27
  • added an edit to this answer, which should help
    – Joey Meere
    Commented Jan 12 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.