1. Trang chủ
  2. » Công Nghệ Thông Tin

Chương 8: Understanding Structed Exception Handling potx

49 303 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 49
Dung lượng 745,5 KB

Nội dung

Chapter 8. Understanding Structured Exception Handling Hoang Anh Viet VietHA@it-hut.edu.vn Hanoi University of Technology 1 Microsoft Outline “This chapter is to discuss how to handle runtime anomalies in your code base through the use of structured exception handling. Not only will you learn about the C# keywords that allow you to handle such problems (try, catch, throw, and finally), but you will also come to understand the distinction between application-level and system- level exceptions. In addition, this chapter examines various tools within Visual Studio 2008 that allow you to debug the exceptions that have escaped your view.”  Introduction  Exception Handling Overview  Example: DivideByZeroException  .NET Exception Hierarchy  finally Block  Exception Properties  Programmer-Defined Exception Classes  Handling Overflows with Operators checked and unchecked 2 Microsoft Roadmap  8.1 Introduction  8.2 Exception Handling Overview  8.3 Example: DivideByZeroException  8.4 .NET Exception Hierarchy  8.5 finally Block  8.6 Exception Properties  8.7 Programmer-Defined Exception Classes  8.8 Handling Overflows with Operators checked and unchecked 3 Microsoft 8.1 Introduction  Definitions for three commonly used anomaly-centric terms  Bugs: errors on the part of the programmer  User errors: Unlike bugs, user errors are typically caused by the individual running your application, rather than by those who created it.  Exceptions: Exceptions are typically regarded as runtime anomalies that are difficult, if not impossible, to account for while programming your application. 4 Microsoft 8.1 Introduction  Exception handling  Create application that can handle or resolve exception  Enable clear, robust and more fault-tolerant programs  .NET structured exception handling is a technique well suited to deal with runtime exceptions.  However, as for the bugs and user errors that have escaped your view, the CLR will often generate a corresponding exception that identifies the problem at hand. 5 Microsoft Roadmap  8.1 Introduction  8.2 Exception Handling Overview  8.3 Example: DivideByZeroException  8.4 .NET Exception Hierarchy  8.5 finally Block  8.6 Exception Properties  8.7 Programmer-Defined Exception Classes  8.8 Handling Overflows with Operators checked and unchecked 6 Microsoft 8.2 Exception Handling Overview  Keywords  Try  Include codes in which exceptions might occur  Catch  Represent types of exceptions the catch can handle  Finally  (Optional) codes present here will always execute  Exception handling  Process synchronous errors  Follows the termination model of exception handling 7 Microsoft 8.2 Exception Handling Overview  Catch type  Must be of class Exception or one that extends it directly or indirectly  Stack unwinding  CLR attempt to locate an enclosing try block in a calling method 8 Microsoft Roadmap  8.1 Introduction  8.2 Exception Handling Overview  8.3 Example: DivideByZeroException  8.4 .NET Exception Hierarchy  8.5 finally Block  8.6 Exception Properties  8.7 Programmer-Defined Exception Classes  8.8 Handling Overflows with Operators checked and unchecked 9 Microsoft 8.3 Example: DivideByZeroException  Error catching  Method Convert.ToInt32 will automatically detect for invalid representation of an integer  Method generates a FormatException  CLR automatic detection for division by zero  Occurrence will cause a DivideByZeroException 10 [...]... End of ThrowExceptionWithCatch Calling ThrowExceptionWithoutCatch In ThrowExceptionWithoutCatch Finally executed in ThrowExceptionWithoutCatch Caught exception from ThrowExceptionWithoutCatch in Main Calling ThrowExceptionCatchRethrow In ThrowExceptionCatchRethrow Message: Exception in ThrowExceptionCatchRethrow Finally executed in ThrowExceptionCatchRethrow Caught exception from ThrowExceptionCatchRethrow... // throws exception and catches it locally public static void ThrowExceptionWithCatch() { Definition for method // try block throws exception try ThrowExceptionWithCatch( { Console.WriteLine( "In ThrowExceptionWithCatch" ); ) 24 UsingExceptions.cs (4/6) } throw new Exception( "Exception in ThrowExceptionWithCatch" ); Create a new Exception Throw statement to throwthrown in try block // catch exception. .. ThrowExceptionCatchRethrow } // end class UsingExceptions Finally block reached but program control returns to first occurrence of a try block Calling DoesNotThrowException In DoesNotThrowException Finally executed in DoesNotThrowException End of DoesNotThrowException Calling ThrowExceptionWithCatch In ThrowExceptionWithCatch Message: Exception in ThrowExceptionWithCatch Finally executed in ThrowExceptionWithCatch... Introduction 8.2 Exception Handling Overview 8.3 Example: DivideByZeroException 8.4 NET Exception Hierarchy 8.5 finally Block 8.6 Exception Properties 8.7 Programmer-Defined Exception Classes 8.8 Handling Overflows with Operators checked and unchecked Microsoft 15 8.4 NET Exception Hierarchy  Net Framework   Class Exception is base class Derived class:  ApplicationException    SystemException  ... method ToString and "exception. ToString(): \n{0}\n", properties Message, StackTrace and exception. ToString() ); InnerException to produce output Console.WriteLine( "exception. Message: \n{0}\n", exception. Message ); Console.WriteLine( "exception. StackTrace: \n{0}\n", exception. StackTrace ); 31 Properties.cs (2/4) Console.WriteLine( "exception. InnerException: \n{0}", exception. InnerException ); } // end... 8.2 Exception Handling Overview 8.3 Example: DivideByZeroException 8.4 NET Exception Hierarchy 8.5 finally Block 8.6 Exception Properties 8.7 Programmer-Defined Exception Classes 8.8 Handling Overflows with Operators checked and unchecked Microsoft 35 8.7 Programmer-Defined Exception Classes  Creating customized exception types    Should derive from class ApplicationException Should end with Exception ... 8.1 Introduction 8.2 Exception Handling Overview 8.3 Example: DivideByZeroException 8.4 NET Exception Hierarchy 8.5 finally Block 8.6 Exception Properties 8.7 Programmer-Defined Exception Classes 8.8 Handling Overflows with Operators checked and unchecked Microsoft 28 8.6 Exception Properties  Properties for a caught exception  Message  Stores the error message associated with an Exception object ... method of class UsingExceptions // process exception returned from ThrowExceptionCatchRethrow catch { Console.WriteLine( "Caught exception from " + "ThrowExceptionCatchRethrow in Main" ); } } // end method Main 23 UsingExceptions.cs (3/6) // no exceptions thrown public static void DoesNotThrowException() Definition for method { DoesNotThrowException( ) // try block does not throw any exceptions try { Console.WriteLine(... be printed" ); } // end method ThrowExceptionWithoutCatch // throws exception, catches it and rethrows it public static void ThrowExceptionCatchRethrow() { // try block throws exception try { Console.WriteLine( "In ThrowExceptionCatchRethrow" ); } throw new Exception( "Exception in ThrowExceptionCatchRethrow" ); // catch any exception, place in object error catch ( Exception error ) { Console.WriteLine(... Console.WriteLine( "\nCalling ThrowExceptionWithCatch" ); ThrowExceptionWithCatch(); Begin try // Case 3: Exception occurs, but not caught // in called method, because no catch handlers Console.WriteLine( "\nCalling ThrowExceptionWithoutCatch" ); block // call ThrowExceptionWithoutCatch try { ThrowExceptionWithoutCatch(); } 22 UsingExceptions.cs (2/6) // process exception returned from ThrowExceptionWithoutCatch . Introduction  8.2 Exception Handling Overview  8.3 Example: DivideByZeroException  8.4 .NET Exception Hierarchy  8.5 finally Block  8.6 Exception Properties  8.7 Programmer-Defined Exception. Introduction  8.2 Exception Handling Overview  8.3 Example: DivideByZeroException  8.4 .NET Exception Hierarchy  8.5 finally Block  8.6 Exception Properties  8.7 Programmer-Defined Exception. Introduction  8.2 Exception Handling Overview  8.3 Example: DivideByZeroException  8.4 .NET Exception Hierarchy  8.5 finally Block  8.6 Exception Properties  8.7 Programmer-Defined Exception

Ngày đăng: 02/08/2014, 09:20

TỪ KHÓA LIÊN QUAN

w