430 likes | 535 Views
Chapter 13 Exception Handling: A Deeper Look. Things Happen. Terminology. Exception Keywords. Running example. using System; using System.Collections.Generic ; using System.Linq ; using System.Text ; namespace UsingExceptions { class Program {
E N D
Running example using System; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceUsingExceptions { classProgram { staticvoid Main(string[] args) { int x = 10; int y = 5; int result; try { result = x / y; Console.WriteLine("The result is: {0}", result); } catch { Console.WriteLine("An error occurred! Better check the code!"); } finally { Console.WriteLine("Just proving that this code always runs."); } Console.ReadLine(); }}}
Hierarchy try { result = x / y; Console.WriteLine("The result is: {0}", result); } catch (System.DivideByZeroException e) { Console.WriteLine("Whoops! You tried to divide by zero!"); Console.WriteLine(e.Message); } catch (System.ArithmeticException e) { Console.WriteLine("Whoops! You tried to divide by zero!"); Console.WriteLine(e.Message); }
More Specific First try { result = x / y; Console.WriteLine("The result is: {0}", result); } catch (System.ArithmeticException e) { Console.WriteLine("Whoops! You tried to divide by zero!"); Console.WriteLine(e.Message); } catch (System.DivideByZeroException e) { Console.WriteLine("Whoops! You tried to divide by zero!"); Console.WriteLine(e.Message); } finally { Console.WriteLine("Just proving that this code always runs."); } Console.ReadLine();
Exceptions are Objects • System.Exception • Constructors • Properties • Hierarchy • http://msdn.microsoft.com/en-us/library/system.systemexception.aspx • System.Exception • System.ApplicationException(non fatal exceptions) • http://msdn.microsoft.com/en-us/library/system.applicationexception.aspx
using System; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceCustomExceptions { publicclassMyOwnException : Exception { publicMyOwnException() : base("Sergey is not welcomed here!") //Exception(string) { //this.HelpLink= "http://msdn.microsoft.com/en-us/library/system.exception.aspx"; } } classProgram { staticstringGetName() { string s = Console.ReadLine(); if (s.Equals("Sergey")) thrownewMyOwnException(); return s; }
staticvoid Main(string[] args) { stringtheName; try { theName = GetName(); Console.WriteLine("Hello {0}", theName); } catch (MyOwnExceptionnje) { Console.WriteLine(nje.Message); Console.WriteLine("For help, visit: {0}", nje.HelpLink); } finally { Console.WriteLine("Have a nice day!"); } Console.ReadLine(); } } }
Re-throwing Exceptions classProgram { staticvoidDoSomeMath() { int x = 10, y = 0; int result; try { result = x / y; Console.WriteLine("Result is {0}", result); } catch { Console.WriteLine("Error in DoSomeMath()"); thrownewArithmeticException(); } } staticvoid Main(string[] args) { try { DoSomeMath(); } catch (ArithmeticException e) { Console.WriteLine("Hmm, there was an error in there, be careful!"); } Console.ReadLine(); } }
13.3 Example: Handling DivideByZeroExceptions and FormatExceptions