Last active
September 23, 2018 18:54
-
-
Save janellbaxter/9838bb29198dd0b1b8e86e26b062fab1 to your computer and use it in GitHub Desktop.
Try/catch example for Introduction to Programming class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace TryCatchExample | |
{ | |
class MyMath | |
{ | |
public int DivideBySelf(int _number) | |
{ | |
int _result = 0; | |
try | |
{ | |
_result = _number / _number; | |
Console.WriteLine(_result); | |
} | |
catch (DivideByZeroException) | |
{ | |
Console.WriteLine("0 cannot be divided by 0"); | |
} | |
return _result; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace TryCatchExample | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
int result; | |
Console.WriteLine("Welcome to my app that divides a number by itself."); | |
Console.WriteLine("Enter number:"); | |
int number = Convert.ToInt16(Console.ReadLine()); | |
MyMath math = new MyMath(); | |
result = math.DivideBySelf(number); | |
if (result!=0) | |
Console.WriteLine("Pretty slick getting back a " + result + " from dividing " + number + " by itself"); | |
Console.WriteLine("press any key to exit..."); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment