Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 898059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:58:53+00:00 2026-05-15T14:58:53+00:00

In a JSP file I’m getting a: Type expected (found ‘try’ instead) Error while

  • 0

In a JSP file I’m getting a:

Type expected (found 'try' instead)

Error while attempting to set up a connection. This leaves me with two questions. What is going wrong here? and more generally, what causes ‘Type Expected’ Errors in JSP? Since I can’t find an explanation of the error in a Google search. Here’s the code.

<%!
class ThisPage extends ModernPage
{
     try{
        Connection con=null;
        PreparedStatement pstmt=null;
        con = HomeInterfaceHelper.getInstance().getConnection();
        pstmt = con.prepareStatement("sql goes here");
        ResultSet rs = pstmt.executeQuery();
        con.close();  
    }
    catch (Exception e){
        System.out.println("sql error: exception thrown");
    }
}
%>

Edited to show more code

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:58:54+00:00Added an answer on May 15, 2026 at 2:58 pm

    Usually you can’t add a try .. catch block inside a class declaration, you should at least put it inside a method like the constructor of the class or a static { } block.

    I don’t know if JSP’s syntax is different but did you try something like:

    class ThisPage extends ModernPage {
      Connection con;
      PreparedStatement pstmt;
    
    
      ThisPage() {
        try{
          con=null;
          pstmt=null;
          con = HomeInterfaceHelper.getInstance().getConnection();
          pstmt = con.prepareStatement("sql goes here");
          ResultSet rs = pstmt.executeQuery();
          con.close();  
        }
        catch (Exception e){
            System.out.println("sql error: exception thrown");
        }
      }
    }
    

    If you look at Java Language Specification you can see that a TryStatement cannot be inserted inside a class declaration..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While writing a .jsp file, can i include all the html5 tags in it
I'm having some issues getting my pdf file to display in my jsp page.
I am building MVC CRUD application. In JSP file I got this error The
This is my jsp file body tag. <body> <jsp:plugin type=applet code=SApplet.class codebase=C:\Program Files\apache-tomcat-6.0.18\webapps\testweb\WEB-INF\classes width=400
In my a.jsp file: <html> <body> <% out.print(<my:login error=\${Error}\/>); // It doesn't work :(
I have a jsp file were the db connection is created and queries can
I have the following JSP File and Servlet file <html> <head> <meta http-equiv=Content-Type content=text/html;
JSP CODE: Upload File: <input name=file type=file id=file><br><br> <input type=submit name=Submit value=Submit/><br><br> <input type=reset
Hi i have a JSP file , while trying to add code i got
I'm receiving this error on my jsp file - 'Can not find the tag

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.