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

  • Home
  • SEARCH
  • 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 7944803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:39:41+00:00 2026-06-04T00:39:41+00:00

Anyone know how to fix the following errors unreported exception javax.naming.NamingException; must be caught

  • 0

Anyone know how to fix the following errors

unreported exception javax.naming.NamingException; must be caught or declared to be thrown
Context context = new InitialContext();

Auth.java:46: unreported exception java.sql.SQLException; must be caught or declared to be thrown conn = ds.getConnection();

that I get from this java servlet?

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.sql.SQLException;
import oracle.jdbc.OracleTypes;

public class ABC extends HttpServlet {

  @Override
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
  }

  protected void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {

    Connection conn;
    CallableStatement cs;

    String xy = req.getParameter("xy");
    String zz = req.getParameter("zz");

    // call stored procedure
    Context context = new InitialContext();
    DataSource ds = (DataSource)context.lookup("jdbc/mypool");
    conn = ds.getConnection();
    cs = conn.prepareCall( "{call mysproc (?,?)}" );
    cs.setString(1, xy);
    cs.setString(2, zz);
    cs.execute();

    if ( conn != null ) {
      try { conn.close(); } catch ( Exception ex ) {}
      conn = null;
    }

    // Set the content type (MIME Type) of the response.
    res.setContentType("text/html");

    // Write the HTML to the response
    PrintWriter out = res.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>my title</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h2>my header</h2>");
    out.println("my body text<br/>");
    out.println("</body>);
    out.println("</html>");
    out.flush();
    out.close();
  }

  public void destroy() {  
  }         
}

If I try to replace

  protected void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {

with

  protected void doGet(HttpServletRequest req, HttpServletResponse res)
    throws Exception {

or

  protected void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException, SQLException, NamingException {

they both produce errors saying I cannot override doGet, as overridden method does not throw Exception, or SQLException, or NamingException.

  • 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-06-04T00:39:43+00:00Added an answer on June 4, 2026 at 12:39 am

    new InitialContext() throws a checked exception NamingException, which should either be caught or the method where you are using this code should have a throws clause associated with it.

    Since you are extending HttpServlet and overriding doGet method you cannot attach new checked Exception, as it is against the law of overriding in Java.

    Instead place the code inside a try catch block and catch NamingException.

    So instead of

    Context context = new InitialContext();
    

    replace this by

    Context context = null;
    try {
        context = new InitialContext();
    } catch(NamingException exp){
        //Handle Exception
    }
    

    Similarly dataSource.getConnection throws a checked exception SQLException which should be caught or rethrown, once again you cannot add new checked exception to your doGet method because of rules of overriding you will have to catch it explicitly.

    try {
        DataSource ds = (DataSource)context.lookup("jdbc/mypool");
        conn = ds.getConnection();
        cs = conn.prepareCall( "{call mysproc (?,?)}" );
        cs.setString(1, xy);
        cs.setString(2, zz);
        cs.execute();
    
    } catch ( SQLException exp ) {
      //Handle your exception
    } finally {  
      if (conn != null ) {
          try {
             conn.close(); 
          } catch(SQLException sqlExp){
             // Handle your exception     
          }
          conn = null;
        }
    }
    

    Rules of Overriding in Java:

    Overridden Method

    • Arguments Must not change
    • Return type Can’t change except for covariant (subtype) returns
    • Exceptions Can reduce/eliminate. Must not throw new/broader checked exceptions
    • Access Must not be more restrictive. Can be less restrictive.
    • Invocation Which method to call is based on object type, at runtime time
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

anyone know how to fix the following error? Retrieving the COM class factory for
Dose anyone know how to fix this error? I installed zorba and it worked
anyone know how to fix this? this is what i get when i try
Does anyone know how to fix this warning? MyMain.java:12: warning: [unchecked] unchecked conversion found
Anyone know how I can make a login button in Ruby on Rails for
Anyone know how to set the custom 404 Error pages that the Visual Studio
Anyone know if there is a standard way to create a List from an
Anyone know of a tutorial online that explains how to integrate alexbilbie CodeIgniter-OAuth-2.0-Server as
Anyone know an example of phonegap with $.ajax() in github for Android? I have
Anyone know how to do draw sth similar to this in Java and working

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.