Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anything wrong with throwing exceptions in the try block?

Tags:

c#

Is it a good design practice to code a try-catch block as follows? That is, use a throw in the try block and then catch it in the catch block.

try
{
  if (someCondition){

      throw new Exception("Go the the associated catch block!");

     }
}

catch(Exception ex)
{
      logError("I was thrown in the try block above");
}
like image 937
Utpal Mattoo Avatar asked May 14 '12 18:05

Utpal Mattoo


People also ask

Is it good practice to throw exception in try block?

(For exceptions) Be careful of how much code you put in your try blocks. It's considered best practice to put as little code as possible in each try / catch block. This means that you may need multiple try / catch blocks, instead of just one.

What happens if we throw an exception in try block?

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 kind of statements should not be put inside a try block?

Explanation: The statements which may cause problems are put in try block. Also, the statements which should not be executed after a problem accursed, are put in try block. Note that once an exception is caught, the control goes to the next line after the catch block.

Can we throw exception from TRY block in Java?

The caller has to handle the exception using a try-catch block or propagate the exception. We can throw either checked or unchecked exceptions. The throws keyword allows the compiler to help you write code that handles this type of error, but it does not prevent the abnormal termination of the program.


4 Answers

Exceptions are for exceptional circumstances. It should not be used as control of flow logic but if there is a edge case that needs to be handled if it does come up there is nothing wrong in doing it.

I have done this myself throwing a InvalidDataException in my code if the data I am reading is not what I am expecting.

like image 29
Scott Chamberlain Avatar answered Oct 13 '22 00:10

Scott Chamberlain


In general, it is not bad design, if it is the shortest writable method. But beware, that throwing an excaption usually takes approximetaly 1 ms to catch. In that matter it is a performance issue.

like image 29
bytecode77 Avatar answered Oct 13 '22 01:10

bytecode77


There are times when you might want to - for example, if you're using ado.net, it has a habit of throwing everything as a SqlException - you might want to catch some of these, and handle them, whilst leaving the handling of others to another level. In this case, you'd have to catch the SqlException, see if you can handle it, and rethrow it if not.

like image 193
Rowland Shaw Avatar answered Oct 13 '22 00:10

Rowland Shaw


It depends, you should throw exception in an usual situation and is better your own exception rather than a general one.

like image 1
Asif Mushtaq Avatar answered Oct 13 '22 00:10

Asif Mushtaq