How do I programmatically create a route?
I think that in your case the problem is in this line:

    '/example/',


that should be:

    '/example',


so without the final /.

You can try with the other method extending from RouteSubscriberBase and your routes method needs to receive as a parameter a RouteCollection.

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

class ExampleRoutes extends RouteSubscriberBase {

  public function routes(RouteCollection $collection) {

  }

}


And you need to define the Route Subscriber as a service

  example.route_subscriber:
    class: Drupal\example\Routing\ExampleRoutes
    tags:     - { name: event_subscriber }


Take a look at this example: 

Dynamic Routes in Drupal 8 with a RouteSubscriber