• Post Reply Bookmark Topic Watch Topic
  • New Topic

try-catch block  RSS feed

 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
can anyone explain why the try block should immedately be folllowed by the catch block . ie what if we guve some statements between the try and the catch
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try block must be immediately followed by catch or finally or catch and then finally block .
catch block is for catch any exception that arise in try . finally block is for cleanup/close any resource which is open in try

any statement between try and catch/finally break this above mentioned activities. compiler will complain about it.
shortly, Java wont allow you to do that
 
Punya Pratap Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its actually programming building blocks provided by java that you have to follow them.we always use try when we expect that the code we have written in try{} block may throw an exception .so if it happens(Exception is thrown) there will be no Execution after that our only responsibility left is, to control program .
So for good program design we use catch or finally right after a try block.
If you write any statement after try block it will give you compile time error.
because java compiler would not allow you to do that.

you just consider that java sells these blocks together--in one unit.

[
try{}
catch(){}
or
finally{}
(or both together)
]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!