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)
{
}
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • 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. – Peter Lawrey Sep 19 '13 at 10:23
  • Try Catch Performance, possible duplicate : http://stackoverflow.com/q/16451777/1544069 – ritesh Sep 19 '13 at 10:33

3 Answers3

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.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
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)
{
}
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • Any advantage of not using exception but to use only try realted exception – SpringLearner Sep 19 '13 at 10:38
  • 2
    @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 – SpongeBobFan Sep 19 '13 at 10:40
  • @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 – Richard Tingle Sep 19 '13 at 10:43
  • @SpongeBobFan with respect to performance I think both take same time? – SpringLearner Sep 19 '13 at 10:54
  • @javaBeginner i'm sure, yes, no worries. Exceptions is not part of performance scope – Maxim Shoustin Sep 19 '13 at 10:57
  • 1
    @javaBeginner, why do you think about performance? Is it a bottleneck of your application? – SpongeBobFan Sep 19 '13 at 10:58
  • @javaBeginner Any considerations of performance over readability before profiling is premature optimisation. **Often 1% of your program will take 90% of the time.** Optimising the performance of any part of your program other than the 1% is pointless and likely to reduce the readability of your program and risk introducing bugs. – Richard Tingle Sep 19 '13 at 11:26
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

Community
  • 1
  • 1
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24