Running code on exit

Just wanted to verify this is a safe way of running code when the program exits:

function setup() {}
function draw() {}

function dispose() {
  // Anything else I want cleaned up

Didn’t see any official way in the documentation - this works, but since it’s not documented anywhere I’d like to make sure there’s nothing nefarious going on in the background.

Poking through the processing source, it looks like an alternative is

public class Disposer {
  public void dispose() {
      print("dispose handler");

setup() {
  registerMethod("dispose", new Disposer());

Is that preferred? They seem to do the same thing as far as I can tell