Skip to content

Instantly share code, notes, and snippets.

@janellbaxter
Last active September 23, 2018 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janellbaxter/9838bb29198dd0b1b8e86e26b062fab1 to your computer and use it in GitHub Desktop.
Save janellbaxter/9838bb29198dd0b1b8e86e26b062fab1 to your computer and use it in GitHub Desktop.
Try/catch example for Introduction to Programming class
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;
}
}
}
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