Trap Exception in POSTGRESQL script

I want to run a script inside a transaction in postgresql. So I suround the sql with begin , commit statements. But I want to rollback on error. I dont see how to do that.

BEGIN; UPDATE public.tablename SET blah = ‘xxx’ WHERE thing= ‘123’;

COMMIT;