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 8507859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:59:18+00:00 2026-06-11T02:59:18+00:00

My requirement is to send automatic mail whenever the exception arises public Connection dbConnect(String

  • 0

My requirement is to send automatic mail whenever the exception arises

public Connection dbConnect(String dbconnectionstring,String userid,String userpassword)
{
    Connection connection = null;
    System.out.println();
  try {
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
     connection = DriverManager.getConnection(dbconnectionstring,userid,userpassword);
     System.out.println("connected to" + dbconnectionstring );

    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

  return connection;

now in the other class, if I want to access the content of Exception e (if any exception raises), what should be written in my code to know if any exception raised? and if the exception raised, the content of e should be mailed.

  • 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-11T02:59:20+00:00Added an answer on June 11, 2026 at 2:59 am

    It depends what “other” is. If it is the caller of your doConnect() method just do not catch the exceptions but throw them and catch in caller method:

    public void doConnect(....) throws InstantiationException, IllegalAccessException, .......{
    }
    
    public void theCaller() {
        try {
        doConnect(......)
        } catch(InstantiationException e) {
        }
        } catch(IllegalAccessException e) {
        }
    /// etc
    }
    

    BTW in java 7 you can catch all exceptions in one block:

    } catch(IllegalAccessException | InstantioationException | SqlException e)

    Alternatively you can wrap specific exception using other exception that can be either checked or unchecked (runtime), e.g.

    public Connection dbConnect(String dbconnectionstring,String userid,String userpassword)
    {
        Connection connection = null;
        System.out.println();
      try {
         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
         connection = DriverManager.getConnection(dbconnectionstring,userid,userpassword);
         System.out.println("connected to" + dbconnectionstring );
    
        } catch (InstantiationException | IllegalAccessException  | ClassNotFoundException  | SQLException e) {
            throw new IllegalStateException("Connection failure", e);
        }
    
      return connection;
    }
    

    Now the caller may catch IllegalStateException and extract its cause.

    If however you need some kind of asynchronous listener that manages all exceptions you can implement one class like ExceptionManager that is being updated by other classes when exception happens there. The ExceptionManager may be singleton, so the code will look like this:

    public void doConnect() {
        try {
           doConnect(......)
        catch(IllegalAccessException | InstantiationException | SqlException e)
            ExceptionManager.getInstance().exceptionThrown(e);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All,In my requirement need to send a mail with attached text file, which is
My Requirement is whenever I click on the Extension Icon that should send a
I use the following script to send mail using PHP. Now my requirement is
My requirement is that I want to send email from MS SQL Server whenever
New to twitter api. Requirement: Need to send direct messages to twitter followers of
As per client requirement i want to send crash Report when App get crash.
I have a requirement to design an application which notifies/publishes, its subsystems to send
I have a requirement to send the results of a query in emails. I
My application has a requirement to open the send email window in outlook. The
The requirement of the TCP server: receive from each client and send result back

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.