To cover a so-called terahertz gap in available sources of coherent electromagnetic radiation, the gyrotron with a pulsed solenoid producing up to a 40 T magnetic field has been designed, manufactured, and tested. At a 38.5 T magnetic field, the gyrotron generated coherent radiation at 1.022 THz frequency in 50 musec pulses. The microwave power and energy per pulse were about 1.5 kW and 75 mJ, respectively. Details of the gyrotron design, manufacturing, operation and measurements of output radiation are given.