Correct way to handle exceptions in Java

Zeus

I have a singleton connector object that helps in connecting to a mysql database.

 public class Connector{
        private Connector(){

            }
            public static Connector getInstance(){
                if(unique == null) unique = new Connector();
                return unique;
            }

            public void connect() throws SQLException{

                conn = (Connection) DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
                if(conn != null) System.out.println("Connected");
            }

            public void close() throws SQLException{
                if(conn != null) conn.close();

            }
    }

But naturally I'm bubbling up the exceptions for the caller to handle. This is my calling client.

Connector connector = Connector.getInstance();
        try {
            connector.connect();
        } catch (SQLException e) {
            System.out.println("Connected now");
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            connector.close();
            System.out.println("Connection closed");
        }

Now the connector doesn't compile as it wants me to wrap the finally within a try catch since the method close() also throws an exception. This would mean I'd have to write the code like.

finally {
            try {
                connector.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("Connection closed");
        }

This somehow doesn't look right. What's the right way to deal with this situation. Thanks in anticipation.

callyalater

Use the decorator pattern to handle this. You can change your Connector class to implement AutoCloseable (which just has one method: public void close()) and ensure that there is no exception thrown. Then you can use a try-with-resources when working with Connector.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Correct way to handle exceptions in Spring Boot

Python: Correct way to handle chain exceptions

Preferred way to handle Java exceptions in ServletContextListener

The correct way to handle Exceptions in a Python2.7 context manager class

Recommended way to handle exceptions?

Is this correct way to handle an exception?

What is the cleanest/correct way to handle Future.get() custom exceptions inside callable? (Advanced)

What is the correct way to handle exceptions in a REST API that produces an Excel or PDF file as response?

Rails way to catch and handle exceptions

What is preferable way to handle exceptions?

What is the cleaner way to handle exceptions

Correct way of throwing exceptions with Reactor

Correct way to handle deprecated API

Correct way to handle deadlocks in Hibernate

handle exceptions in stream java 8

Best way to handle exceptions that can not occur

Is there a way to handle exceptions automatically with Python Click?

Proper way to handle exceptions in Ruby on Rails

How to handle Python exceptions in a decent way?

What is the best way to handle exceptions in this scenario

Correct way to document bubbled exceptions in C#?

java - is there a standard way of collecting exceptions?

Whats the correct Go way to do handle errors

Correct way to handle incorrect URL when opening

correct way to handle logout from table row

What is the correct way to handle stale NSURL bookmarks?

Correct way to place and handle .json file in Xcode

Which is the correct way to handle Form submission in React?

Correct way to handle conditional styling in React