Question

I am working on program , at one point of coding I have code which will generate exception Every time and i need to do that.

In other sense I want to use catch block more than try block.

I want to know this kind of coding can create problem in Apllication (in future after deployment) as there is always Exception Occurs?

Is it harmful?

try{
//throws exception every time
}
catch(Exception e)
{
//coding//Important part of code
}

Well I have other solution but what I want to know is PROBLEMS caused by this kind of Code.

I agree This is wrong but my Question is What are problems which can be created by this??

Was it helpful?

Solution

What you are trying to do is infinitely wrong!!! For you have not posted any details, I'll assume you are throwing different exceptions and then do something according to type of the thrown exception.

A better approach is to return a value (maybe an enum) and do processing according to it's value rather than the type of the thrown exception. There are of course better approaches to the problem, but I cannot say anything speciffic unless you give more details about what you are trying to do.

Eceptions should be used only in exceptional cases because:

  1. Everybody expects that
  2. No one would expect your use-case
  3. Exceptions are slow compared to a switch on enum for instance
  4. It's unintuitive and plain wrong
  5. The people who will maintain your code will hate you :)

OTHER TIPS

Can it create a problem? The simple answer is that it already is one. If your code throws an Exception every time, without fail, then why not get rid of the try-catch altogether? I highly doubt that what your code is doing in the try block cannot be structured any other way. As @user16547 mentioned, Exceptions should be exceptional. They were not designed to be used as an integral part of your application logic, or a perverted goto. Specific reasons not to do this:

  • Throwing an Exception is costly, and you will seriously hurt your application if this is performance critical code.

  • This hurts readability a good deal, as most people will not expect your code to be structured this way.

  • It is generally a bad idea to catch the general Exception class, as this only further obfuscates your code, and masks other potential errors. Consider that some part of your code fails in the try block that you did not expect to. It still reverts to the catch, and could potentially cause your entire application to crash!

Without seeing your actual code, it is difficult to determine whether what you're doing is justified. However, as a general rule, this is not good practice, and is detrimental to performance, readability, and reliability.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top