DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Containers Trend Report. Explore the current state of containers, containerization strategies, and modernizing architecture.

ChatGPT is fun, but the future is fully autonomous AI for code. Learn why the tech behind LLMs is good for some tasks but not others.

Trending

  • Build a Serverless Application for Image Label Detection
  • Implement OpenAPI for AWS Lambda in Quarkus Using API Gateway Integrator
  • Data Warehouses: The Undying Titans of Information Storage
  • Terraform Explained in Five Minutes

Trending

  • Build a Serverless Application for Image Label Detection
  • Implement OpenAPI for AWS Lambda in Quarkus Using API Gateway Integrator
  • Data Warehouses: The Undying Titans of Information Storage
  • Terraform Explained in Five Minutes
  1. DZone
  2. Data Engineering
  3. Databases
  4. Error Handling via GOTO in C

Error Handling via GOTO in C

In this post, a database expert takes a look at how to better deal with error handling in a SQLite codebase while working in the C language.

Oren Eini user avatar by
Oren Eini
·
Jul. 05, 18 · Analysis
Like (1)
Save
Tweet
Share
3.43K Views

Join the DZone community and get the full member experience.

Join For Free

Following up on my previous post, I was asked about the use of GOTO in C for error handling.

I decided to go and look at a bunch of C code bases, and it took me no time at all to find some interesting examples of this approach.

Below, you can see a small portion from a random method in the SQLite codebase. SQLite is considered to be one of the best C code bases out there, in terms of code quality, number, and thoroughness of tests and the quality of the project as a whole. As such I think it is a good candidate for a sample.

Here you can see there is the need to free some resources and at the same time, handle errors. This is routinely handled using a cleanup code at the end of the function that error clauses will jump to if needed.

image

Note that this is very different from “GOTO considered harmful.” That article talked about jumping to any arbitrary location in the program. C (and all other modern languages) limit you to jump around only inside your own method. Even though methods are quite long in the SQLite codebase, it is very easy to follow what is going on there.

Here is another example, which is a lot more complex. I have taken it from the Linux kernel code:

image

Again, I had to redact a lot of code that actually do stuff to allow us to look at the error handling behavior. Now you can see something that is a lot more complex.

There are actually multiple labels here, and they jump around between one another in the case of an error. For example, if we fail to allocate the cifs_sb we jump to out_nls, which then jump to out. Failure to get the root will jump us to out_super and then fall into out. For out_free, it falls to out_nls and then jump to out.

In both the SQLite example and the Linux example, we have two separate and related responsibility. We need to do resource cleanup, but that is complicated by the different stages that this may go through in the function. Given that, GOTO cleanup is the best option available, I think.

There are various defer style options for C, libdefer, and defer macro looks better. But both have some runtime costs. I like the defer macro better, but that won’t work on MSVC, for example, as it requires a language extension.

Linux kernel SQLite Macro (computer science) POST (HTTP) Testing Label

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Build a Serverless Application for Image Label Detection
  • Implement OpenAPI for AWS Lambda in Quarkus Using API Gateway Integrator
  • Data Warehouses: The Undying Titans of Information Storage
  • Terraform Explained in Five Minutes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: