How can I access the exception raised from another class

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.

Answers


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);
    }
}

Re-throw the same exception, or wrap it inside an application-specific exception class (my preference):

    ...
} catch (InstantiationException e) {
    ...
    throw new MyAppException(e);
}

Need Your Help

Why isn't this code writing log output on my Tridion Content Delivery server?

tridion tridion-2011 tridion-content-delivery

I have a user control rendering content from a custom (non-Tridion) database. The connection string for this custom database is incorrect, and so I'm getting an SqlException when the code tries to

Make a placeholder div overlappable dynamically

css dynamic positioning css-position overlap

I'm using a jQuery UI accordion as a menu on the right side of a page. However, there are occasions where the menu will be taller than the content on the left. Because of this, when expanding/contr...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.