Download presentation
Presentation is loading. Please wait.
Published byFrederica Booth Modified over 9 years ago
2
Pemrograman VisualMinggu …12… Page 1 MINGGU Ke Duabelas Pemrograman Visual Pokok Bahasan: Exception Handling Tujuan Instruksional Khusus: Mahasiswa dapat menjelaskan dan mengaplikasikan Exception handling pada Visual Basic 2008 Referensi: Deitel Deitel, Visual Basic 2008 (2009), How to Program, Prentice Hall. Chapter 13
3
Pemrograman VisualMinggu …12… Page 2 Agenda Exception handling Overview Divide by Zero Without Exception Handling Handling DivideByZeroExceptions and Format Exceptions.NET Exception Hierarchy Exception Properties User-Defined Exception Classes
4
Pemrograman VisualMinggu …12… Page 3 Exception Handling Overview An exception is an indication of a problem that occurs during a program’s execution. Exception handling enables you to create applications that can resolve (or handle) exception. Intermixing program logic with error handling logic can make programs difficult to read, modify, maintain and debug, especially in large applications. Exception handling enables you to remove error- handling code from the “main line” of the program’s execution, improving program clarity and enhancing modifiability. Visual basic enables you to deal with exception handling easily from the beginning of a project.
5
Pemrograman VisualMinggu …12… Page 4 Divide by Zero Without Exception Handling An exception is thrown when a method or the CLR detects a problem and is unable to handle it. A stack trace includes the name of the exception in a descriptive message that indicates the problem that occurred and the complete method call stack at the time the exception occurred. Division by zero is not allowed in integer arithmetic. When division by zero occurs in integer arithmetic, a DivideByZeroException is thrown. A FormatException occurs when Convert method ToInt32 receives a string that does not represent a valid integer. See Example figure 13_04
6
Pemrograman VisualMinggu …12… Page 5 Handling DivideByZeroExceptions and Format Exceptions
7
Pemrograman VisualMinggu …12… Page 6.NET Exception Hierarchy The Visual basic exception handling mechanism allows objects only of class Exception and its derived classes to be thrown and caught. Class Exception of namespace System is the base class of the.NET Framework Class Library exception class hierarchy. The CLR generates SystemExceptions, which can occur at any point during the execution of the program. Many of these Exceptions can be avoided if applications are coded properly. A benefit of using the exception class hierarchy is that a catch block can catch exceptions of a particular type or because of the is a relationship of inheritance can use a base class type to catch exceptions in a hierarchy of related exception types. A catch block that specifies an exception parameter of type exception can catch all exceptions that derive from exception, because exception is the base class of all exception classes. Using inheritance with exceptions enables an exception handler to catch related exceptions using a concise notation. See Example figure 13_01
8
Pemrograman VisualMinggu …12… Page 7 Exception Properties Property message of class exception stores the error message associated with an Exception object. Property StackTrace of class Exception contains a string that represents the method call stack. Another exception used frequently by class library programmers is InnerException. Typically, you use this property to “Wrap” exception objects caught in your code so that you then can throw new exception types specific to your libraries. When an exception is thrown but not caught in a particular scope, the method call stack is “unwound” and an attempt is made to catch the exception in the next outer try block—this is known as stack unwinding.. See Example figure 13_05
9
Pemrograman VisualMinggu …12… Page 8 User-Defined Exception Classes User-defined exception class should derive directly or indirectly from class exception of namespace system. User-defined exceptions should typically extend Exception, have a class name that ends with “Exception” and define a parameterless constructor, a constructor that receives a string argument (the error message), and a constructor that receives a string argument and an exception argument (the error message and the inner exception object). See Example figure 13_06-07
10
Pemrograman VisualMinggu …12… Page 9 Questions & Answers
11
Pemrograman VisualMinggu …12… Page 10 Thank You
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.