Download presentation
Presentation is loading. Please wait.
Published byDavid McDonald Modified over 9 years ago
1
Exception Handling SWE 344 Internet Protocols & Client Server Programming
2
Exception Handling Exceptions are unforeseen errors that happen in our programs. Most of the time, we can, and should, detect and handle program errors in our code. For example, validating user input, checking for null objects, and verifying the values returned from methods are what we expect, are all examples of good standard error handling that we should be doing all the time. However, there are times when we don't know if an error will occur. For example, we can't predict when we will receive a file I/O error, run out of system memory, or encounter a database error. These things are generally unlikely, but they could still happen and we want to be able to deal with them when they do occur. This is where exception handling comes in. When exceptions occur, they are said to be "thrown". What is actually thrown is an object that is derived from the System.Exception class.
3
The System.Exception class provides several methods and properties for obtaining information on what went wrong. For example, the Message property provides summary information about what the error was, the Stacktrace property provides information from the stack for where the problem occurred, and the ToString() method is overridden to reveal a verbose description of the entire exception. Identifying the exceptions we'll need to handle depends on the routine we're writing. For example, if the routine opened a file with the System.IO.File.OpenRead() method, it could throw any of the following exceptions: SecurityException ArgumentException ArgumentNullException PathTooLongException DirectoryNotFoundException UnauthorizedAccessException FileNotFoundException NotSupportedException
4
When exceptions are thrown, we need to be able to handle them. This is done by implementing a try/catch block. Code that could throw an exception is put in the try block an exception handling code goes in the catch block. try/catch Blocks
5
Example #1: try/catch Blocks using System; using System.IO; class tryCatchDemo { static void Main(string[] args) { try { File.OpenRead("myfile.txt"); } catch { Console.WriteLine("sorry...file not found"); } Console.ReadLine(); } sorry...file not found
6
Example #2: try/catch Blocks namespace nsDivZero { using System; public class DivZero { public static void Main() { // Set an integer equal to 0 int IntVal1 = 0; // and another not equal to zero int IntVal2 = 57; try { Console.WriteLine("{0} / {1} = {2}", IntVal2, IntVal1, IntResult(IntVal2, IntVal1)); } catch (DivideByZeroException e) { Console.WriteLine(e.Message); } // Set a double equal to 0 double dVal1 = 0.0; double dVal2 = 57.3;
7
try { Console.WriteLine("{0} / {1} = {2}", dVal2, dVal1, DoubleResult(dVal2, dVal1)); } catch (DivideByZeroException e) { Console.WriteLine(e.Message); } Console.ReadLine(); } static public int IntResult(int num, int denom) { return (num / denom); } static public double DoubleResult(double num, double denom) { return (num / denom); } Example #2: try/catch Blocks Attempted to dived by zero 57.3 / 0 = infinity
8
END
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.