Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Try Catch or check value first

I need to know what is better (for performance and else), Using Try Catch when expecting error and there is no alternate scenario when error hapened or check the value first ?

For Example (VB.net): If I need to fill text box by value from database (from Data Reader for example), and I expecting some null values, and if value is null I will leave textbox empty.

now I have to ways to do this:

try
  textbox1.text = DR("Name")
catch
end try

now if column Name contain null value the error will raise and textbox1 still empty.

If Not IsDBNull(DR("Name")) Then textbox1.Text = DR("Name")

what is better ?

Thank you so much.

like image 795
Prince Waleed Avatar asked Feb 20 '26 03:02

Prince Waleed


1 Answers

Do not use exception handling as flow control. Ever.

So - check the value instead of Try/Catch.

When an exception is thrown the runtime has quite a lot of work to do - if you expect null values - always check for them instead of relying on exceptions.

like image 50
Oded Avatar answered Feb 21 '26 16:02

Oded



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!