Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning PostgreSQL 10 - Second Edition

You're reading from  Learning PostgreSQL 10 - Second Edition

Product type Book
Published in Dec 2017
Publisher
ISBN-13 9781788392013
Pages 488 pages
Edition 2nd Edition
Languages

Table of Contents (23) Chapters

Title Page
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface
Relational Databases PostgreSQL in Action PostgreSQL Basic Building Blocks PostgreSQL Advanced Building Blocks SQL Language Advanced Query Writing Server-Side Programming with PL/pgSQL OLAP and Data Warehousing Beyond Conventional Data Types Transactions and Concurrency Control PostgreSQL Security The PostgreSQL Catalog Optimizing Database Performance Testing Using PostgreSQL in Python Applications Scalability

Exception handling


One could trap and raise errors in PostgreSQL using the exception and raise statements. Errors can be raised by violating data integrity constraints, or by performing illegal operations such as assigning text to integers, dividing an integer or float by zero, out-of-range assignments, and so on. By default, any error occurrence inside a PL/pgSQL function causes the function to abort the execution and roll back the changes. To be able to recover from errors, PL/pgSQL can trap the errors using the EXCEPTION clause. The syntax of the exception clause is very similar to PL/pgSQL blocks. Moreover, PostgreSQL can raise errors using the RAISE statement. To understand exception handling, let's consider the following helping function:

CREATE OR REPLACE FUNCTION check_not_null (value anyelement ) RETURNS VOID AS
$$
BEGIN
  IF (value IS NULL) THEN RAISE EXCEPTION USING ERRCODE = 'check_violation'; END IF;
END;
$$ LANGUAGE plpgsql;

The check_not_null statement is a polymorphic function...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}