2

for sql connection we generally do like the following way

Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","username","password"); 

As this throws exception so we generally write inside try block.But my question if i write unnecessary codes that does not throws exception with in try block then will the performance gets effected?

try { //some 100 lines codes that does not throw exception Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","username","password"); } catch(Exception e) { } 
2
  • 1
    If you can't delete the code, then you need it. I would worry about performance if you have measured that it is impacting your application. Otherwise you are just guessing. Commented Sep 19, 2013 at 10:23
  • Try Catch Performance, possible duplicate : stackoverflow.com/q/16451777/1544069 Commented Sep 19, 2013 at 10:33

3 Answers 3

2

No, at least not in any meaningful way. The biggest harm in having a "too big" try block is readability and in most cases it doesn't make sense. You could just have a throws SQLException in the method instead of a try block that spans most of the method.

Sign up to request clarification or add additional context in comments.

Comments

1

Don't think so.

Its good practice to save your application from crashes.

But idea is to do not blindly use Exception but exception refer to your try{}.

In your case:

try{ //... } catch (ClassNotFoundException e) { } catch(SQLException e) { } 

7 Comments

Any advantage of not using exception but to use only try realted exception
@javaBeginner, catch(Exception e) means that you don't know what you're trying to catch. In a good application you want to deal with different exceptions different ways. If you don't want to deal with exceptions in your method, you can just throw another exception according to abstraction level to catch it in another place. In your case, ClassNotFoundException will be thrown if something is wrong with jdbc driver, but SQLException will be thrown if there's an error with username, password, connection to database server, with the server itself
@javabeginner presumably your catch deals with a specific problem that throws out an SQLException. If an unexpected error gets into that catch the SQL fix will be applied to a non SQL problem leading to "unexpected" results. I am assuming you have something in the catch statement in real life and just omitted it for brevity in your question <looks stern>
@SpongeBobFan with respect to performance I think both take same time?
@javaBeginner, why do you think about performance? Is it a bottleneck of your application?
|
1

If I write unnecessary codes that does not throws exception with in try block then will the performance gets effected?

Answer is no .
Having code in try-blocks should have basically zero performance effect. The real hit comes when an exception is actually thrown.
Read these SO questions
1.Java try/catch performance, is it recommended to keep what is inside the try clause to a minimum?
2.Try Catch Performance Java

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.