VB.Net - Exceptions Copyright © Martin Schray 1997- 2003.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
Error Handling with Exceptions Concepts C and other earlier languages often had multiple error-handling schemes, and these were generally established.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
E XCEPTION H ANDLING Chapter 11 C S 442: A DVANCED J AVA P ROGRAMMING.
Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
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.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
Exceptions. 2 Objectives Introduce C# exception handling –library exception types –custom exceptions Describe keywords used for exception handling –try.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
 2006 Pearson Education, Inc. All rights reserved Exception Handling.
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
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.
Object Oriented Programming
CIS 270—Application Development II Chapter 13—Exception Handling.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Slides Credit Umair Javed LUMS Web Application Development.
Java Programming: Guided Learning with Early Objects
Introduction to Exception Handling and Defensive Programming.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exceptions, handling exceptions & message boxes Year 11 Information Technology.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Exception Handling in C++. Outline What exceptions are and when to use them Using try, catch and throw to detect, handle and indicate exceptions, respectively.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
JavaScript and Ajax (Control Structures) Week 4 Web site:
C# Exceptions 1 CNS 3260 C#.NET Software Development.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
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.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Lecture 11 Dr. Eng. Ibrahim El-Nahry Exception Handling.
16 Exception Handling.
Java Programming Language
Chapter 14: Exception Handling
CNS 3260 C# .NET Software Development
Part B – Structured Exception Handling
Chapter 11: Exception Handling
Exception Handling.
Presentation transcript:

VB.Net - Exceptions Copyright © Martin Schray

Exceptions During execution runtime errors can occur There are different levels of severity of runtime errors: warnings and errors Checking for all possible errors is a good practice, but not very practical (can even create code clutter)

Exceptions Exceptions provide a clean way to check for errors “without” cluttering code Exceptions provide a mechanism for signaling errors directly rather then relaying on checking return codes or state variables –Getting a return code back to a caller can be tough –State vars don’t work well with threading

Exceptions Exceptions are a great way to provide information to developers using of a library (e.g. using Collection Namespace to store objects) Exceptions provide far more useful information then just a return code –can provide information about the methods called up until the exception (call stack) –Useful text message describing the exception

Exceptions An exception is thrown when an “unexpected” error occurs An exception is caught by an code waiting for that exception or a group of exceptions to be thrown

Exception we seen so far... On some occasions our programs don’t seem to run or run only partially …hmmm.. Looking at the VB.NET output window you see a runtime error message (e.g. null object or exceeded array bounds) This is an uncaught exception

Try Catch Syntax Try [ tryStatements ] [ Catch [ exception [ As type ] ] [ When expression ] [ catchStatements ] ] [ Exit Try ]... [ Finally [ finallyStatements ] ] End Try

Try Catch Syntax described Try –start try catch block Catch –each Catch statement is examined in order to determine if it handles the exception (can be multiple catch statements) Finally –Optional. A Finally block is always executed when execution leaves any part of the Try statement. End Try –Terminates the Try...Catch...Finally structure.

Non-exception code example int myInt = 0; myInt = myInt/0; ‘ whoops division by zero!

Exception technique example int myInt = 0; Try myInt= 1/0; Catch diverr As DivideByZeroException MsgBox("division by zero is not defined") End Try

Exception technique example with finally int myInt = 0; Try myint = 1/0; Catch diverr As DivideByZeroException MsgBox("division by zero is not defined") Finally textbox1.text = “” ‘ make sure result is cleared End Try

Explanation of Example try block encloses code that might generate an exception Each try block is immediately followed by one (or more) catch blocks Each catch block specifies the type of exception it can catch and an exception handler After the try completes successfully OR exception if thrown (whether it is caught or not) the finally is executed

What happens when a exception occurs When an exception is thrown, control leaves the try block The catch blocks are searched in order for “a match” If the thrown exception matches the parameter list for a catch that catch is executed Execution continues after last catch Or No catch matches and exception bubbles up

What happens when NO exception occurs The code in the try block is completely executed The catch blocks are skipped Execution resumes after the last catch block

Affects on execution If the code in a try block throws an exception will immediately leave the try and look for a matching catch If a catch block parameter matches the thrown exception execution continues in the exception handler and then after the last catch block

Exception and inheritance Various exceptions classes can be derived from a common superclass (System.Exception) If a catch is written to catch a superclass it will also catch of subclass of that superclass ()

Getting information about exceptions Exceptions have two properties that are useful for discovering information about an exception  StackTrace - prints the method call stack  Message - gets the text message associated with an exception

How to throw an exception Find the appropriate exception class Make an object of that class throw it Dim e As New System.DivideByZeroException() Throw e

Making your own exception classes You will use inheritance Select the appropriate type of exception classes to inherit from (probably SystemException) System.ApplicationException System.SystemExceptionSystem.ApplicationExceptionSystem.SystemException ….

Stack trace Arhhhh… what is it??? Usually there a tons of lines that seem to be deep within the innards of VB.NET (makes sense we are using BCL class libraries) Look for the names of classes that you have written Read from the top down (to see the last line executed) Read from the bottom up (if you need to follow program flow ~ call stack)

Stack traces Typically include the line number of the calls as part of the stack trace