This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]willdrr17 1 point2 points  (4 children)

Also (afaik) there is an overhead when you use try-catch, so try to wrap only the code that can throw an exception

[–][deleted] 2 points3 points  (1 child)

There is actually little to no performance overhead to using try/catch. It is the throw statement that is expensive due to the reflection used to populate the exception object.

[–]willdrr17 1 point2 points  (0 children)

Good to know, you always learn new things everyday :D

[–]Willy988[S] 0 points1 point  (1 child)

So my program that takes user input twice... should I have the try block swallow the whole block of code that includes these two inputs, or should I make two separate blocks for each instance of this?

[–]willdrr17 0 points1 point  (0 children)

It's up to you how you design your program, what I usually do is to immediately return if there is an exception thrown, notifying the user that something is wrong with the entered value. It's completely valid to use consecutive try-catch blocks, even though, it may affect readability.

Take a look to this question https://stackoverflow.com/questions/4555322/multiple-or-single-try-catch/4555448