![Page 1: Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within](https://reader035.vdocument.in/reader035/viewer/2022062421/56649cba5503460f94981a8a/html5/thumbnails/1.jpg)
![Page 2: Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within](https://reader035.vdocument.in/reader035/viewer/2022062421/56649cba5503460f94981a8a/html5/thumbnails/2.jpg)
Try…Catch…Finally Blocks (continued)
• Generic catch clause– Omit argument list with the catch– Any exception thrown is handled by executing
code within that catch block
• Control is never returned into the try block after an exception is thrown
• Using a try…catch block can keep the program from terminating abnormally
![Page 3: Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within](https://reader035.vdocument.in/reader035/viewer/2022062421/56649cba5503460f94981a8a/html5/thumbnails/3.jpg)
Use of Generic Catch clause
Example 11-2Uses a generic catch block
![Page 4: Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within](https://reader035.vdocument.in/reader035/viewer/2022062421/56649cba5503460f94981a8a/html5/thumbnails/4.jpg)
What caused these exceptions to be thrown?
Never quite sure what causes theexception to be thrown
when a generic catch
clause is used!
![Page 5: Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within](https://reader035.vdocument.in/reader035/viewer/2022062421/56649cba5503460f94981a8a/html5/thumbnails/5.jpg)
Exception Object
• When an exception is raised, an object is created
– Object has properties and behaviors (methods)
• Catch clause may list an exception class
– Catch { } without exception type does not give you access to an object
• Base exception class—Exception
– Message property returns a string describing exception
– StackTrace property returns a string that contains the called trace of methods
![Page 6: Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within](https://reader035.vdocument.in/reader035/viewer/2022062421/56649cba5503460f94981a8a/html5/thumbnails/6.jpg)
Exception Object (continued)
catch (System.Exception e) { Console.Error.WriteLine("Problem with scores - " + "Can not compute average"); Console.Error.WriteLine(e.Message);}
![Page 7: Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within](https://reader035.vdocument.in/reader035/viewer/2022062421/56649cba5503460f94981a8a/html5/thumbnails/7.jpg)
Exception Classes
•ApplicationException and SystemException classes form the basis for runtime exceptions
![Page 8: Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within](https://reader035.vdocument.in/reader035/viewer/2022062421/56649cba5503460f94981a8a/html5/thumbnails/8.jpg)
Exception Classes (continued)
• ApplicationException
– Derive from this class when you write your own exception class
– User program must throw the exception—not the CLR
• SystemException
– Most runtime exceptions derive from this class
– SystemException class adds no functionality to classes
• Includes no additional properties or methods
![Page 9: Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within](https://reader035.vdocument.in/reader035/viewer/2022062421/56649cba5503460f94981a8a/html5/thumbnails/9.jpg)
SystemException Classes
Over 70 classes derive from the SystemExceptio
n class
![Page 10: Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within](https://reader035.vdocument.in/reader035/viewer/2022062421/56649cba5503460f94981a8a/html5/thumbnails/10.jpg)
System.DivideByZeroException
• Derived class of System.ArithmeticException class
• Thrown when an attempt to divide by zero occurs
• Only thrown for integral or integer data types
• Floating-point operands do not throw an exception
– Result reported as either positive infinity, negative infinity, or Not-a-Number (NaN)
– Follows the rules from IEEE 754 arithmetic
![Page 11: Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within](https://reader035.vdocument.in/reader035/viewer/2022062421/56649cba5503460f94981a8a/html5/thumbnails/11.jpg)
Filtering Multiple Exceptions
• Can include multiple catch clauses
• Enables writing code specific to thrown exception
• Should be placed from most specific to the most generic
• If Exception class is included, it should always be placed last
![Page 12: Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within](https://reader035.vdocument.in/reader035/viewer/2022062421/56649cba5503460f94981a8a/html5/thumbnails/12.jpg)
Custom Exceptions
• Derive from the ApplicationException class
![Page 13: Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within](https://reader035.vdocument.in/reader035/viewer/2022062421/56649cba5503460f94981a8a/html5/thumbnails/13.jpg)
Custom Exceptions (continued)
• Throwing a programmer-defined exception
– Exception object is instantiated when "an exceptional condition occurs”
– Can be any condition, but should be one that happens infrequently
– After object is instantiated, object is thrown
![Page 14: Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within](https://reader035.vdocument.in/reader035/viewer/2022062421/56649cba5503460f94981a8a/html5/thumbnails/14.jpg)
Throwing an Exception
static double GetResults(double value1, double value2){ if (value2 < .0000001) // Careful comparing floating-point values // for equality { FloatingPtDivisionException e = new FloatingPtDivisionException ("Exception type: Floating point division by zero"); throw e; } return value1 / value2;}
![Page 15: Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within](https://reader035.vdocument.in/reader035/viewer/2022062421/56649cba5503460f94981a8a/html5/thumbnails/15.jpg)
Input Output (IO) Exceptions
• System.IO.IOException
– Direct descendent of Exception
– Thrown when a specified file or directory is not found
– Thrown when program attempts to read beyond the end of a file
– Thrown when there are problems loading or accessing the contents of a file
![Page 16: Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within](https://reader035.vdocument.in/reader035/viewer/2022062421/56649cba5503460f94981a8a/html5/thumbnails/16.jpg)
Input Output (IO) Exceptions (continued)