Huazhong University of Science and Technology has been developing a proton therapy facility based on a superconducting cyclotron. As an important section of the facility, the gantry beamline has been designed using TRANSPORT code with simplified fringing field model and linear transfer matrix. However, realistic magnetic fringing field and second-order terms of particle motion cannot be precisely described in this approach, which may deviate the actual result from the design value and thereby increase uncertainties for radiation therapy during beam delivery. For this consideration, the specific effects of fringing field and second-order aberrations have been investigated using OPERA code and ODE solvers. For the potential problems of the designed gantry beamline, COSY INFINITY code, which can calculate transfer matrix of any order under the realistic fringing field represented by Enge function, was used to optimize the optics of present gantry beamline. The final results suggest the beam analysis and optimization methods mentioned in this paper are effective and valuable for relevant applications.