Just Starting A Degree In Computer Science, MyVB Question (Beginner)

I am also learning Java and learning it easily. Teacher is great and takes his time. Can't say the same for MyVB teacher. We're learning Try Catch and he only read a Power Point riddle with typos. He converts PDFs to Word Docs and doesn't spell check. Can I get an explanation about a Try Catch for doing a calculation button for a 10% increase followed by a 10% decrease in salary? Pardon if the explanation isn't clear, but that's about as clear as it is for me. Thanks if you can help!

Posted Sep 25, 2017, 7:27 PM UTC

37
secret_porn_acct•

Try...catch is for error handling so you can handle certain type of errors that you are expecting to happen, rather than having the application crash.

1
type0nullOP•

But the math formula is written after the Try, correct? And I do know to declare variables at the very start I believe.

1
secret_porn_acct•

Yes. That would make sense.

1
TheFotty•

What is MyVB? You used it in the title and the text. Are you saying "my VB question and my VB teacher" or "MyVB"?

1
type0nullOP•

Lol whoops I'm saying it wrong. We use a thing called MyProgrammingLab and I combined the names and didn't realize haha. I mean Visual Basic.

1
TheFotty•

Gotcha.

A Try/Catch/Finally block is just a structure of code that allows you to create exception handling. Errors in .NET (among other languages/runtimes) are called exceptions, so that is how I will refer to them, but it just means error. The Try block contains the code you are going to TRY to execute. It is the main code you are trying to run to accomplish whatever task the piece code is supposed to carry out. The catch block contains the code that should run only if an exception is thrown by the code in the try block, for example trying to convert a non numeric string like "abc" into a number.

Dim x as integer = convert.tointeger("abc") 

Will throw an exception because "abc" is not a number.

Dim x as integer = convert.tointeger("123")

will work though, because it can convert a string of "123" to an integer of 123 without an error occuring.

The Finally block is where you put code that you want to run no matter what. Meaning if code in the Try block runs and doesn't throw any exceptions, the catch block will never be called and the code in it will no run, but the code in the finally clause will run. The same if an exception is hit and the catch block of code is run, finally will still run. the Finally part of a try/catch/finally is generally used to clean up objects and free up any possible used resources (like closing an open handle to a file on the system, or closing out a database connection, etc..)

3
type0nullOP•

Thank you so much. That is way, way clearer. Finally was never even mentioned so this is a big help. Thanks again.

1