Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complete code in try/catch block

Tags:

java

c++

I want to know, is it a good practice to place complete code inside a try block or I should place only the code which I feel it will cause a specific exception?
And should I catch basic Exception always

Code 1: complete code in try block

myFunction(){
 try{
   .........
   Code with chance of OneException 
   .............
 }catch(OneException e){
  ............
 }catch(Exception e){
   ..............
 }
}

Code 2: Only the Code with chance of Exception in try block

myFunction(){
  .......
  try{
   Code with chance of OneException 
  }catch(OneException e){
  ............
  }
  ............
}

Code 3:Should I catch Exception always

    myFunction(){
      .......
      try{
       Code chance of OneException 
      }catch(OneException e){
      ............
      }catch(Exception e){
       ..............
      }
      ........
   } 

Out of this (code1, code2 and code3) which one is the best?
I'm mainly concern with java and C++ coding

like image 715
Labeeb Panampullan Avatar asked Jul 28 '11 09:07

Labeeb Panampullan


People also ask

How do you enter a code on try catch block?

Java try and catchThe try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

Which code goes in the try block?

Try block contains the code that might throw an exception. Catch block contains the exception handler for exceptions in the try block. The finally block contains the critical code that will execute regardless of whether the exception has occurred or not.

Is code executed after try catch?

The code in the try block is executed first, and if it throws an exception, the code in the catch block will be executed.

What is the correct syntax of the try catch block?

Explanation: Try-catch block has the following syntax: try{ // codes that needs to check for exceptions } catch(Exception E1){ // codes for handling exception.... // Exception E denotes the type of exception this block is handling. } catch(Exception E2){ // other exception that needs to be handled... }


2 Answers

Generally speaking, you should only catch exceptions you're interested in and which you can handle. That is...catch an exception where you can do something s.t. the user doesn't perceive the problem or when it is explicitly necessary to tell the user about the problem.
For all other exceptions, let them pop up with all their details (stacktrace etc..) which you obviously log. Note, obviously this doesn't mean the user should also see that exception output but rather a generic error.

Told this, I assume that when you write "Code chance of OneException" you know how to handle OneException, but not Exception, right? So then...only handle OneException.

like image 168
Juri Avatar answered Sep 23 '22 07:09

Juri


Always catch exactly what you have to and no more. No matter how much we try, we cannot make our code completely "idiot proof". If someone passes you something which will cause some random error, then it is their job to handle it. If our code handles someone else's exception that has far too much risk of being an unexpected side-effect.

As far as what code to place where: code before the line which could throw the Exception will be run either way, so it does not really make sense to have it inside the try block and before the code which throws. Code after the potential exception should be placed between try and catch if and only if it depends on the exception generating code. So, if your database connection call can fail, place all of the database queries inside the try block.

Limiting the "time" spent in a try...catch makes it easier to read and less prone to accidental catching. I can't tell you how many hours have been lost because someone decided to catch an Exception which should have propagated.

like image 30
cwallenpoole Avatar answered Sep 24 '22 07:09

cwallenpoole