Is David Tong incorrect in this remark about classical mechanics in his QM lectures? In page 11 of his Quantum Mechanics lectures, we have the following quote:

It turns out that not all classical theories can be written using a Hamiltonian. Roughly speaking, only those theories that have conservation of energy can be formulated in this way.

Now, this seems strange to me. Since the Hamiltonian is simply the Legendre transform of the Lagrangian, there should be Hamiltonians of non-conservative systems as long as there is a Lagrangian for said system. A good example is the electromagnetic Lagrangian. Is it simply a remark that is not meant to be too precise?
 A: Well, in Tong's defense he did insert the phrase roughly speaking to indicate that the remark is not precise.

*

*Concerning that the Lorentz force is not a conservative force according to the traditional definition because its potential is velocity-dependent, I proposed a different definition in my Phys.SE answer here.


*How to provide a Lagrangian or Hamiltonian formulation of dissipative systems is e.g. discussed in this Phys.SE post.


*For examples where the Hamiltonian is not the total energy, see e.g. this & this Phys.SE posts.
A: I would agree with you, that it is not that precise. Because if the failure of conservation of energy comes from the fact that for the participating forces no potential $U$ can be found (i.e. the forces are non-conservative), then one cannot write down any Lagrangian $L=T-U$ and the equation of motion need to be extended with dissipative forces. Thus neither $L$ nor $H$ are sufficient to describe the e.o.m. See for example https://en.wikipedia.org/wiki/Liouville%27s_theorem_(Hamiltonian)#Damped_Harmonic_Oscillator
One the other hand, if the energy is not conserved because although $U$ exists, it is time dependent, one can of course formulate the equation of motions entirely in terms of the Lagrangian and thus the Hamiltonian.
I guess that is why he added the adverb "roughly" at the beginning.
A: Indeed, I think that the statement in Tong's book is quite ambiguous (though it is not definitely false as I discuss below). In principle there is no relation between the possibility of a  Hamiltonian formulation and  conservation of energy. A harmonic oscillator with potential $k(t)x^2/2$ admits Hamiltonian formulation even if $k$ depends on time and thus energy is not conserved.
Physically speaking this system can be obtained by continuously heating the spring of the oscillator, i.e., continuously adding external energy to the system. That is the reason why it is not conserved. However there is a Hamiltonian function, the usual one
$p^2/2m + k(t)x^2/2$, which produces the right equations of motion.
Another example is a material point constrained to stay on a frictionless axis $x$. This axis rotates around the origin $O$ with constant angular velocity $\Omega$ in a inertial reference frame. The material point does not conserve its energy in the inertial reference frame, because energy must be continuously added to preserve the rotation, but the system admits a Hamiltonian formulation with hamiltonian
$H= p^2/2m - m\Omega^2 x^2/2$.
What it is true is that the Hamiltonian formulation is not possible when there are physical forces whose Lagrangian components would change the volume of the space of  the phases during the evolution of the system, thus violating the Liouville theorem. That is the case of dissipative forces. (There is an exception for one-dimensional systems pointed out by @Qmechanic's answer, second point, obtained by exploiting an unconventional approach.) These forces are also an obstruction to energy conservation. I think the book intended just  to  remark this fact by using that "roughly speaking".
However, for macroscopic systems, energy is not conserved also because of a time dependence of some macroscopic parameter describing the system or due to the nature of the constraints. In these cases a Hamiltonian formulation may be still possible.
