Exception Handling.

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Exception Handling Part 1: Handing Java Exceptions CSIS 3701: Advanced Object Oriented Programming.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
MIS 3200 – Unit 6.2 Learning Objectives How to move data between pages – Using Query Strings How to control errors on web pages – Using Try-catch.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
CIS 270—Application Development II Chapter 13—Exception Handling.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
VB.Net - Exceptions Copyright © Martin Schray
COMP Exception Handling Yi Hong June 10, 2015.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
IMS 3253: Validation and Errors 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Validation and Error Handling Validation.
VB.NET and Databases. ADO.NET VB.Net allows you many ways to connect to a database. The technology used to interact with a database or data source is.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Exception Handling. VB.NET has an inbuilt class that deals with errors. The Class is called Exception. When an exception error is found, an Exception.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
C++ Exceptions.
User-Written Functions
similar concepts, different syntax
Handling Exceptionally Sticky Problems
Why exception handling in C++?
Computer Programming I
Introduction to Exceptions in Java
Creating and Modifying Text part 2
Exception Handling.
Chapter 14: Exception Handling
Files and Streams Lect3 CT1411.
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
Exception Handling and
CNS 3260 C# .NET Software Development
Topics Introduction to File Input and Output
Exceptions & Error Handling
Exception Handling.
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling.
Fundamental Error Handling
Exception Handling Oo28.
Part B – Structured Exception Handling
Exception Handling By: Enas Naffar.
ERRORS AND EXCEPTIONS Errors:
Exceptions.
Programming in C# CHAPTER - 7
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Tonga Institute of Higher Education
Exceptions 19-Feb-19.
Problems Debugging is fine and dandy, but remember we divided problems into compile-time problems and runtime problems? Debugging only copes with the former.
Fundaments of Game Design
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Handling Exceptionally Sticky Problems
Exceptions 10-May-19.
Topics Introduction to File Input and Output
Chapter 11: Exception Handling
Exceptions 5-Jul-19.
CMSC 202 Exceptions.
ENERGY 211 / CME 211 Lecture 24 November 14, 2008.
Exceptions Review Checked Vs. Unchecked Exceptions
Lecture Set 9 Arrays, Collections, and Repetition
Presentation transcript:

Exception Handling

Exception Handling VB.NET has an inbuilt class that deals with errors. The Class is called Exception. When an exception error is found, an Exception object is created. The coding structure VB.NET uses to deal with such Exceptions is called the Try … Catch structure. In the coding area for your button, type the word Try. Then hit the return key on your keyboard. VB.NET completes the rest of the structure for you: Try Catch ex As Exception End Try

Exception Handling The Try word means “Try to execute this code”. The Catch word means “Catch any errors here”. The ex is a variable, and the type of variable is an Exception object. When you run your program, VB will Try to execute any code in the Try part. If everything goes well, then it skips the Catch part. However, if an error occurs, VB.NET jumps straight to Catch. Because ex is an object variable, it now has its own Properties and methods. One of these is the Message property

Example Dim x As Integer = 5 Dim y As Integer = 0 Dim z As Integer Try z = x / y MsgBox(z) Catch ex As Exception MsgBox(ex.Message) End Try

fs = new FileStream(FileName, FileMode.Open, FileAccess.Read) Exception Handling fs = new FileStream(FileName, FileMode.Open, FileAccess.Read) FileName = “C:\test10.txt”

Exception Handling The point about this new message box is that it will not crash your program. You have handled the Exception, and displayed an appropriate message for the user. If you know the kind of error that a program might throw, you can get what Type it is from the Error message box you saw earlier. This one:

Click the View Details links under Actions to see the following:

Exception Handling The first line tells us the Type of Exception it is: System.IO.FileNotFoundException You can add this directly to the catch part. Previously, you were just catching any error that might be thrown: Catch ex As Exception But if you know a “file not found” error might be thrown, you can add that to the Catch line, instead of Exception: Catch ex As System.IO.FileNotFoundException You can keep the Exception line as well. (You can have as many Catch parts as you want.) This will Catch any other errors that may occur: Try fs = new FileStream(FileName, FileMode.Open, FileAccess.Read) Catch ex As System.IO.FileNotFoundException MsgBox(“Can’t find this file”) Catch ex As Exception MsgBox(ex.Message) End Try

Exception Handling Try Catch ex As Exception Finally End Try There is one last part of the Try … Catch Statement that VB.NET doesn’t add for you  Finally: Try Catch ex As Exception Finally End Try The Finally part is always executed, whether an error occurs or not. You typically add a Finally part to perform any cleanup operations that are needed. For example, you may have opened a file before going into a Try … Catch Statement. If an error occurs, the file will still be open. Whether an error occurs or not, you still need to close the file. You can do that in the Finally part.