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.

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
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.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
COMP 121 Week 5: Exceptions and Exception Handling.
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
Exception Handling Part 1: Handing Java Exceptions CSIS 3701: Advanced Object Oriented Programming.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
BIM313 – Advanced Programming Techniques Debugging 1.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
11-Jun-15 Exceptions. 2 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.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
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.
How to Debug VB .NET Code.
Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design.
Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get.
CS1101: Programming Methodology Aaron Tan.
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.
Dr. Abraham. Exception Any problem that VB or OS could not handle Robust program A program that performs well not only under ordinary conditions but also.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
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.
Exceptions Handling Exceptionally Sticky Problems.
VB.Net - Exceptions Copyright © Martin Schray
COMP Exception Handling Yi Hong June 10, 2015.
Introduction to Exception Handling and Defensive Programming.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Cem Sahin CS  There are two distinguishable kinds of errors: Python's Errors Syntax ErrorsExceptions.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Event Handling Tonga Institute of Higher Education.
Christina Aiello, 12/3/2015. Reminder: Seen in lecture notes for Exceptions: “Side note: we will NOT expect you to know/remember how to do keyboard input/output.
Error Handling Tonga Institute of Higher Education.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Firoze Abdur Rakib. Syntax errors, also known as parsing errors, are perhaps the most common kind of error you encounter while you are still learning.
CHAPTER 10 ERROR HANDLING & DEBUGGING JavaScript can be hard to learn. Everyone makes mistakes when writing it.
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.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
Introduction to Exceptions in Java CS201, SW Development Methods.
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.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Lecture 18B Exception Handling and Richard Gesick.
Creating and Modifying Text part 2
Exception Handling.
Files and Streams Lect3 CT1411.
Exceptions 10-Nov-18.
Exception Handling and
Chapter 12 Exception Handling and Text IO
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Part B – Structured Exception Handling
Exception Handling.
Exceptions 10-May-19.
Chapter 11: Exception Handling
CMSC 202 Exceptions.
ENERGY 211 / CME 211 Lecture 24 November 14, 2008.
Presentation transcript:

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

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 Exception Handling

Example

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

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: Exception Handling

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

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

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. Exception Handling