Try Catch block
In this lesson we are going to try and familiarize ourselves with a new idea in programming... We are going to push a piece of data from one type of variable into another. In many cases this can be handled easily. In some cases this is much more complicated.
To cast a piece of data from a smaller variable to a larger is easily accomplished. For example moving data from a byte to an int is simple. A byte is a small variable that can store numbers between -255 to 255. An int is a much larger variable and can store numbers from -4294967296 to 4294967295. So moving a number from a byte (small) to an int (larger) is relatively simple. The move from an int (large) to a byte (small) is much more difficult and many times occurs in data loss or crashing our program.
So in this lesson i am going to introduce you to a new chunk of code called a "Try Catch". This "Try Catch" is a chunk of code that will "Try" to do the commands, if there is a problem and Visual Studios tries to throw and exception (error) this block will "Catch" the error and keep our program from crashing. Because we caught the error before it becomes and issue we can then let the user know that there is a problem. This is great when we have users accidentally or maliciously to kill our code.
To cast a piece of data from a smaller variable to a larger is easily accomplished. For example moving data from a byte to an int is simple. A byte is a small variable that can store numbers between -255 to 255. An int is a much larger variable and can store numbers from -4294967296 to 4294967295. So moving a number from a byte (small) to an int (larger) is relatively simple. The move from an int (large) to a byte (small) is much more difficult and many times occurs in data loss or crashing our program.
So in this lesson i am going to introduce you to a new chunk of code called a "Try Catch". This "Try Catch" is a chunk of code that will "Try" to do the commands, if there is a problem and Visual Studios tries to throw and exception (error) this block will "Catch" the error and keep our program from crashing. Because we caught the error before it becomes and issue we can then let the user know that there is a problem. This is great when we have users accidentally or maliciously to kill our code.
The code
Videos
The Assignment
Write a program that does the following
1. Print out a greeting – ask your user to input a number between 500-1000
2. Have the answer be inputted into a string named TestNumber
3. Test the number using a Try Catch block to see if it can be converted into a byte
5. Since the number is too big to be converted into a byte, print out a message in the catch block to the console alerting your user to the problem
1. Print out a greeting – ask your user to input a number between 500-1000
2. Have the answer be inputted into a string named TestNumber
3. Test the number using a Try Catch block to see if it can be converted into a byte
5. Since the number is too big to be converted into a byte, print out a message in the catch block to the console alerting your user to the problem