A tooth has two parts- crown and a root. Now going from outside to inside the crown portion has 3 layers- enamel, dentin and pulp. The root has 3 layers- cementum, dentin and pulp.
[Reference](https://en.m.wikipedia.org/wiki/Human_tooth)
Now among these only pulp is vascular and innervated.
Dentin has got dentinal tubules containing dentinal fluid. Whenever there is any stimulus which has not yet reached the pulp, but may have reached the dentin, eg of stimulus in the form of pressure, temperature change, sweet food, sour food etc, cause the fluid to move or get displaced. The displacement of dentinal fluid stimulates nerve endings in the pulp and hence generates a pain/ sensitivity response to your brain.
![enter image description here](https://i.sstatic.net/oSvFm.jpg)
[Reference](https://www.deardoctor.com/inside-the-magazine/issue-23/treatment-of-tooth-sensitivity/)

![enter image description here](https://i.sstatic.net/HW9AB.jpg)
[Reference](https://www.dentin.co/new-blog/2017/6/9/dentinal-hypersensitivity-hydrodynamic-theory)

>There are three main theories of dentine hypersensitivity: Direct Innervation (DI) Theory Odontoblast Receptor (OR) Theory Fluid Movement/Hydrodynamic Theory

>The Hydrodynamic or Fluid Movement theory is one of the main theories in dentistry to explain the mechanism by which a tooth perceives the sensation of pain. It is currently the most widely accepted theory used to explain tooth sensitivity.

[Reference](https://en.m.wikipedia.org/wiki/Hydrodynamic_theory_dentistry)

Now coming back to your question. As the tooth is cracked, the enamel in that area must have been chipped off and the dentinal tubules and/or pulp must have been exposed.
As mentioned earlier the sweet food also causes the dentinal fluid to be displaced causing a sharp shooting pain as perceived by your brain.
This is because of change is osmolarity. The sweets/sugars changes the osmolarity of dentinal fluid.
>Clinically, osmotic stimuli such as a wide variety of
sweet food, particularly chocolate and sugar syrup, are
one of the main factors causing dentin hypersensitivity.
Previous studies showed that the different stimuli, including osmotic, when applied to dentin caused pain by a hydrodynamic mechanism that involves displacement of 
the dentinal fluid and excites intradental nerve endings.
[Reference](https://www.google.com/url?sa=t&source=web&rct=j&url=http://www.quintpub.com/userhome/jad/jad_2010_02_s0103.pdf&ved=2ahUKEwiP-MLOiLvsAhVezDgGHYqYAhsQFjAAegQIARAB&usg=AOvVaw1uY1pBuzuCpS9K_OYQuyXG)

Hope I have satisfactorily answered your question :)