lecture (19)simple file exceptions – error handling

15
8/14/2019 Lecture (19)Simple file Exceptions – Error handling http://slidepdf.com/reader/full/lecture-19simple-file-exceptions-error-handling 1/15 FSB23103 1 U N I V E R S I T I K U A L A L U M P U R M a l a y s i a F r a n c e I n s t i t u t e Lecture 20 Simple file i/o Exceptions – Error handling Mdm Ratnawati Ibrahim  

Upload: safuanalcatra

Post on 30-May-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture (19)Simple file Exceptions – Error handling

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 115

FSB23103 1

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Lecture 20

Simple file ioExceptions ndash Error handling

Mdm Ratnawati Ibrahim

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 215

FSB23103 2

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Sequential files

These are the most basic type of file that generally holdhuman readable text Accordingly the contents of such filesmay be read using for example Notepad

A sequential file stores items of data one after another eachnew item of data being added to the end of the data alreadystored

A sequential file is used usually whenbull Its not expected to make changes to the data within thefile too often

bull The file information is likely to be processed from start tofinish without the need to jump about within the file to

acquired particular items of databull It is acceptable to add new data to the end of the file

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 315

FSB23103 3

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File class Programs that involve file processing must include

Imports SystemIO

at the beginning The methods of File class

bull OpenTextis used to open an existing fileEg

If the file does not exist then a runtime error will occurbull CreateText

is used to create a new text file or overwrite an existingfile

Eg

FileCreateText (ldquoUhomeunitsco331aNewFiletxtrdquo)

FileOpenText(ldquoUhomeunitsco331aOldFiletxtrdquo)

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 415

FSB23103 4

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Streams

Allow access to a sequence of items stored in a file

The term lsquostreamrsquo is used in the sense of a stream of dataflowing in or out of a program

bull To process data in an existing file it is necessary to

Open the file

Read (input) the data item-by-item from the file into

variables Close the file on completion

bull To transfer data from variables into a file it is necessaryto

Open the file

Output (write) individual items in the requiredsequence to the file

Close the file on completion

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 515

FSB23103 5

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

StreamReader Class

- will be used to read lines of text from a file

The ReadLine method of StreamReader reads a wholeline of text into a string excluding the end-of-linemarkerEg

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 615

FSB23103 6

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

StreamWriter Class

The StreamWriter class has two main methods

Write and WriteLineBoth write a string to file but WriteLine adds an end-of-linemarker after the string Write can also be used with noargument to terminate a line with an end-of-line markerEg

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 715

FSB23103 7

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File searching

Searching a file for an item that satisfies some condition isquite commonEg Suppose we have a file of examination marks obtainedby named students

Smith 43 67

Jones 87 99

Patel 54 32Sutton 65 74

etchellip

and we wish to obtain the marks of a particular student fromthe file and display them

Run

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 815

FSB23103 8

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File searching - example

The process may be expressed in structured English as

found = False

While (more lines to read) And found = False

read line

split line into three fields

If first field matches name Thenfound = True

display other fields in labels

End If

End While

If Not found Then

display a warning

End If

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 915

FSB23103 9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Eg File searching - Code

Ensure appropriate input data present

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1015

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1115FSB23103 11

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Error handling - Exceptions

Is designed to deal with run-time errors such as

bull an attempt to divide by zero (integer division)

bull where memory becomes exhausted

bull an out-of-bounds array index

bull arithmetic overflow

Error handling should be used tobull process only exceptional situations

bull recover from infrequent fatal errors

bull safeguard data and exit a program neatly

bull provide some indication of the fault that has arisen

In general program control using conventional controlstructures is more efficient than using error handlingfacilities but the main logic might become obscured

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1215FSB23103 12

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Exception is a term used in VB to indicate something hasgone wrong

Exceptions are created by being thrown and are detectedby being caught VB uses the keywords Throw Try andCatch to handle these operations

Eg Try and Catch

Private Sub Calculate_Click(helliphelliphelliphelliphellip)Handles CalculateClick

Dim side As Double

Try

side = DoubleParse(txtSideText)

lblDisplayText = ldquoArea is rdquo amp CStr(side side) amp ldquo sq unitsrdquo

Catch exceptionObject As FormatException

lblDisplayText = ldquoError in side re-enterrdquo

End Try

End Sub

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1315FSB23103 13

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Once an exception has been thrown it is possible to provideadditional information by examining the exception objectrsquosMessage and using theToString Method

Eg

bull The message property provides a short string containinga descriptive error message

bull The ToString method returns a string containing severallines providing a trace of the actions that led up to the

exception including the location of where the erroroccurred

Catch exceptionObject As FormatException

MessageBoxShow(exceptionObjectMessage)

MessageBoxShow(exceptionObjectToString())

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1415FSB23103 14

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Eg Error handling

In the case of division by zero by floating-point divisionVBNet outputs the word lsquoinfinityrsquo

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1515FSB23103 15

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

Files and exceptions

File input-output is a major source of exceptions eg

bull incorrect file namebull Disk full

bull CD removed while reading in progress

The FileOpenText method can throw a number of exceptions Inparticular FileNotFoundExceptionEg Program 2 may be modified to include

Catch problem As FileNotFoundException

MessageBoxShow(ldquoError - ldquo amp txtFileText amp _

ldquo file not found Re-enter namerdquo)

Catch problem As Exception

MessageBoxShow(ldquoError ndash concerning file ldquo amp _

txtFileText amp ldquo ldquo amp problemmessage())

End Try

Page 2: Lecture (19)Simple file Exceptions – Error handling

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 215

FSB23103 2

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Sequential files

These are the most basic type of file that generally holdhuman readable text Accordingly the contents of such filesmay be read using for example Notepad

A sequential file stores items of data one after another eachnew item of data being added to the end of the data alreadystored

A sequential file is used usually whenbull Its not expected to make changes to the data within thefile too often

bull The file information is likely to be processed from start tofinish without the need to jump about within the file to

acquired particular items of databull It is acceptable to add new data to the end of the file

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 315

FSB23103 3

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File class Programs that involve file processing must include

Imports SystemIO

at the beginning The methods of File class

bull OpenTextis used to open an existing fileEg

If the file does not exist then a runtime error will occurbull CreateText

is used to create a new text file or overwrite an existingfile

Eg

FileCreateText (ldquoUhomeunitsco331aNewFiletxtrdquo)

FileOpenText(ldquoUhomeunitsco331aOldFiletxtrdquo)

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 415

FSB23103 4

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Streams

Allow access to a sequence of items stored in a file

The term lsquostreamrsquo is used in the sense of a stream of dataflowing in or out of a program

bull To process data in an existing file it is necessary to

Open the file

Read (input) the data item-by-item from the file into

variables Close the file on completion

bull To transfer data from variables into a file it is necessaryto

Open the file

Output (write) individual items in the requiredsequence to the file

Close the file on completion

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 515

FSB23103 5

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

StreamReader Class

- will be used to read lines of text from a file

The ReadLine method of StreamReader reads a wholeline of text into a string excluding the end-of-linemarkerEg

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 615

FSB23103 6

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

StreamWriter Class

The StreamWriter class has two main methods

Write and WriteLineBoth write a string to file but WriteLine adds an end-of-linemarker after the string Write can also be used with noargument to terminate a line with an end-of-line markerEg

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 715

FSB23103 7

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File searching

Searching a file for an item that satisfies some condition isquite commonEg Suppose we have a file of examination marks obtainedby named students

Smith 43 67

Jones 87 99

Patel 54 32Sutton 65 74

etchellip

and we wish to obtain the marks of a particular student fromthe file and display them

Run

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 815

FSB23103 8

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File searching - example

The process may be expressed in structured English as

found = False

While (more lines to read) And found = False

read line

split line into three fields

If first field matches name Thenfound = True

display other fields in labels

End If

End While

If Not found Then

display a warning

End If

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 915

FSB23103 9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Eg File searching - Code

Ensure appropriate input data present

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1015

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1115FSB23103 11

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Error handling - Exceptions

Is designed to deal with run-time errors such as

bull an attempt to divide by zero (integer division)

bull where memory becomes exhausted

bull an out-of-bounds array index

bull arithmetic overflow

Error handling should be used tobull process only exceptional situations

bull recover from infrequent fatal errors

bull safeguard data and exit a program neatly

bull provide some indication of the fault that has arisen

In general program control using conventional controlstructures is more efficient than using error handlingfacilities but the main logic might become obscured

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1215FSB23103 12

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Exception is a term used in VB to indicate something hasgone wrong

Exceptions are created by being thrown and are detectedby being caught VB uses the keywords Throw Try andCatch to handle these operations

Eg Try and Catch

Private Sub Calculate_Click(helliphelliphelliphelliphellip)Handles CalculateClick

Dim side As Double

Try

side = DoubleParse(txtSideText)

lblDisplayText = ldquoArea is rdquo amp CStr(side side) amp ldquo sq unitsrdquo

Catch exceptionObject As FormatException

lblDisplayText = ldquoError in side re-enterrdquo

End Try

End Sub

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1315FSB23103 13

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Once an exception has been thrown it is possible to provideadditional information by examining the exception objectrsquosMessage and using theToString Method

Eg

bull The message property provides a short string containinga descriptive error message

bull The ToString method returns a string containing severallines providing a trace of the actions that led up to the

exception including the location of where the erroroccurred

Catch exceptionObject As FormatException

MessageBoxShow(exceptionObjectMessage)

MessageBoxShow(exceptionObjectToString())

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1415FSB23103 14

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Eg Error handling

In the case of division by zero by floating-point divisionVBNet outputs the word lsquoinfinityrsquo

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1515FSB23103 15

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

Files and exceptions

File input-output is a major source of exceptions eg

bull incorrect file namebull Disk full

bull CD removed while reading in progress

The FileOpenText method can throw a number of exceptions Inparticular FileNotFoundExceptionEg Program 2 may be modified to include

Catch problem As FileNotFoundException

MessageBoxShow(ldquoError - ldquo amp txtFileText amp _

ldquo file not found Re-enter namerdquo)

Catch problem As Exception

MessageBoxShow(ldquoError ndash concerning file ldquo amp _

txtFileText amp ldquo ldquo amp problemmessage())

End Try

Page 3: Lecture (19)Simple file Exceptions – Error handling

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 315

FSB23103 3

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File class Programs that involve file processing must include

Imports SystemIO

at the beginning The methods of File class

bull OpenTextis used to open an existing fileEg

If the file does not exist then a runtime error will occurbull CreateText

is used to create a new text file or overwrite an existingfile

Eg

FileCreateText (ldquoUhomeunitsco331aNewFiletxtrdquo)

FileOpenText(ldquoUhomeunitsco331aOldFiletxtrdquo)

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 415

FSB23103 4

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Streams

Allow access to a sequence of items stored in a file

The term lsquostreamrsquo is used in the sense of a stream of dataflowing in or out of a program

bull To process data in an existing file it is necessary to

Open the file

Read (input) the data item-by-item from the file into

variables Close the file on completion

bull To transfer data from variables into a file it is necessaryto

Open the file

Output (write) individual items in the requiredsequence to the file

Close the file on completion

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 515

FSB23103 5

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

StreamReader Class

- will be used to read lines of text from a file

The ReadLine method of StreamReader reads a wholeline of text into a string excluding the end-of-linemarkerEg

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 615

FSB23103 6

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

StreamWriter Class

The StreamWriter class has two main methods

Write and WriteLineBoth write a string to file but WriteLine adds an end-of-linemarker after the string Write can also be used with noargument to terminate a line with an end-of-line markerEg

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 715

FSB23103 7

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File searching

Searching a file for an item that satisfies some condition isquite commonEg Suppose we have a file of examination marks obtainedby named students

Smith 43 67

Jones 87 99

Patel 54 32Sutton 65 74

etchellip

and we wish to obtain the marks of a particular student fromthe file and display them

Run

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 815

FSB23103 8

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File searching - example

The process may be expressed in structured English as

found = False

While (more lines to read) And found = False

read line

split line into three fields

If first field matches name Thenfound = True

display other fields in labels

End If

End While

If Not found Then

display a warning

End If

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 915

FSB23103 9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Eg File searching - Code

Ensure appropriate input data present

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1015

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1115FSB23103 11

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Error handling - Exceptions

Is designed to deal with run-time errors such as

bull an attempt to divide by zero (integer division)

bull where memory becomes exhausted

bull an out-of-bounds array index

bull arithmetic overflow

Error handling should be used tobull process only exceptional situations

bull recover from infrequent fatal errors

bull safeguard data and exit a program neatly

bull provide some indication of the fault that has arisen

In general program control using conventional controlstructures is more efficient than using error handlingfacilities but the main logic might become obscured

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1215FSB23103 12

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Exception is a term used in VB to indicate something hasgone wrong

Exceptions are created by being thrown and are detectedby being caught VB uses the keywords Throw Try andCatch to handle these operations

Eg Try and Catch

Private Sub Calculate_Click(helliphelliphelliphelliphellip)Handles CalculateClick

Dim side As Double

Try

side = DoubleParse(txtSideText)

lblDisplayText = ldquoArea is rdquo amp CStr(side side) amp ldquo sq unitsrdquo

Catch exceptionObject As FormatException

lblDisplayText = ldquoError in side re-enterrdquo

End Try

End Sub

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1315FSB23103 13

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Once an exception has been thrown it is possible to provideadditional information by examining the exception objectrsquosMessage and using theToString Method

Eg

bull The message property provides a short string containinga descriptive error message

bull The ToString method returns a string containing severallines providing a trace of the actions that led up to the

exception including the location of where the erroroccurred

Catch exceptionObject As FormatException

MessageBoxShow(exceptionObjectMessage)

MessageBoxShow(exceptionObjectToString())

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1415FSB23103 14

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Eg Error handling

In the case of division by zero by floating-point divisionVBNet outputs the word lsquoinfinityrsquo

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1515FSB23103 15

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

Files and exceptions

File input-output is a major source of exceptions eg

bull incorrect file namebull Disk full

bull CD removed while reading in progress

The FileOpenText method can throw a number of exceptions Inparticular FileNotFoundExceptionEg Program 2 may be modified to include

Catch problem As FileNotFoundException

MessageBoxShow(ldquoError - ldquo amp txtFileText amp _

ldquo file not found Re-enter namerdquo)

Catch problem As Exception

MessageBoxShow(ldquoError ndash concerning file ldquo amp _

txtFileText amp ldquo ldquo amp problemmessage())

End Try

Page 4: Lecture (19)Simple file Exceptions – Error handling

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 415

FSB23103 4

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Streams

Allow access to a sequence of items stored in a file

The term lsquostreamrsquo is used in the sense of a stream of dataflowing in or out of a program

bull To process data in an existing file it is necessary to

Open the file

Read (input) the data item-by-item from the file into

variables Close the file on completion

bull To transfer data from variables into a file it is necessaryto

Open the file

Output (write) individual items in the requiredsequence to the file

Close the file on completion

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 515

FSB23103 5

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

StreamReader Class

- will be used to read lines of text from a file

The ReadLine method of StreamReader reads a wholeline of text into a string excluding the end-of-linemarkerEg

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 615

FSB23103 6

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

StreamWriter Class

The StreamWriter class has two main methods

Write and WriteLineBoth write a string to file but WriteLine adds an end-of-linemarker after the string Write can also be used with noargument to terminate a line with an end-of-line markerEg

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 715

FSB23103 7

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File searching

Searching a file for an item that satisfies some condition isquite commonEg Suppose we have a file of examination marks obtainedby named students

Smith 43 67

Jones 87 99

Patel 54 32Sutton 65 74

etchellip

and we wish to obtain the marks of a particular student fromthe file and display them

Run

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 815

FSB23103 8

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File searching - example

The process may be expressed in structured English as

found = False

While (more lines to read) And found = False

read line

split line into three fields

If first field matches name Thenfound = True

display other fields in labels

End If

End While

If Not found Then

display a warning

End If

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 915

FSB23103 9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Eg File searching - Code

Ensure appropriate input data present

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1015

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1115FSB23103 11

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Error handling - Exceptions

Is designed to deal with run-time errors such as

bull an attempt to divide by zero (integer division)

bull where memory becomes exhausted

bull an out-of-bounds array index

bull arithmetic overflow

Error handling should be used tobull process only exceptional situations

bull recover from infrequent fatal errors

bull safeguard data and exit a program neatly

bull provide some indication of the fault that has arisen

In general program control using conventional controlstructures is more efficient than using error handlingfacilities but the main logic might become obscured

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1215FSB23103 12

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Exception is a term used in VB to indicate something hasgone wrong

Exceptions are created by being thrown and are detectedby being caught VB uses the keywords Throw Try andCatch to handle these operations

Eg Try and Catch

Private Sub Calculate_Click(helliphelliphelliphelliphellip)Handles CalculateClick

Dim side As Double

Try

side = DoubleParse(txtSideText)

lblDisplayText = ldquoArea is rdquo amp CStr(side side) amp ldquo sq unitsrdquo

Catch exceptionObject As FormatException

lblDisplayText = ldquoError in side re-enterrdquo

End Try

End Sub

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1315FSB23103 13

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Once an exception has been thrown it is possible to provideadditional information by examining the exception objectrsquosMessage and using theToString Method

Eg

bull The message property provides a short string containinga descriptive error message

bull The ToString method returns a string containing severallines providing a trace of the actions that led up to the

exception including the location of where the erroroccurred

Catch exceptionObject As FormatException

MessageBoxShow(exceptionObjectMessage)

MessageBoxShow(exceptionObjectToString())

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1415FSB23103 14

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Eg Error handling

In the case of division by zero by floating-point divisionVBNet outputs the word lsquoinfinityrsquo

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1515FSB23103 15

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

Files and exceptions

File input-output is a major source of exceptions eg

bull incorrect file namebull Disk full

bull CD removed while reading in progress

The FileOpenText method can throw a number of exceptions Inparticular FileNotFoundExceptionEg Program 2 may be modified to include

Catch problem As FileNotFoundException

MessageBoxShow(ldquoError - ldquo amp txtFileText amp _

ldquo file not found Re-enter namerdquo)

Catch problem As Exception

MessageBoxShow(ldquoError ndash concerning file ldquo amp _

txtFileText amp ldquo ldquo amp problemmessage())

End Try

Page 5: Lecture (19)Simple file Exceptions – Error handling

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 515

FSB23103 5

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

StreamReader Class

- will be used to read lines of text from a file

The ReadLine method of StreamReader reads a wholeline of text into a string excluding the end-of-linemarkerEg

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 615

FSB23103 6

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

StreamWriter Class

The StreamWriter class has two main methods

Write and WriteLineBoth write a string to file but WriteLine adds an end-of-linemarker after the string Write can also be used with noargument to terminate a line with an end-of-line markerEg

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 715

FSB23103 7

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File searching

Searching a file for an item that satisfies some condition isquite commonEg Suppose we have a file of examination marks obtainedby named students

Smith 43 67

Jones 87 99

Patel 54 32Sutton 65 74

etchellip

and we wish to obtain the marks of a particular student fromthe file and display them

Run

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 815

FSB23103 8

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File searching - example

The process may be expressed in structured English as

found = False

While (more lines to read) And found = False

read line

split line into three fields

If first field matches name Thenfound = True

display other fields in labels

End If

End While

If Not found Then

display a warning

End If

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 915

FSB23103 9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Eg File searching - Code

Ensure appropriate input data present

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1015

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1115FSB23103 11

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Error handling - Exceptions

Is designed to deal with run-time errors such as

bull an attempt to divide by zero (integer division)

bull where memory becomes exhausted

bull an out-of-bounds array index

bull arithmetic overflow

Error handling should be used tobull process only exceptional situations

bull recover from infrequent fatal errors

bull safeguard data and exit a program neatly

bull provide some indication of the fault that has arisen

In general program control using conventional controlstructures is more efficient than using error handlingfacilities but the main logic might become obscured

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1215FSB23103 12

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Exception is a term used in VB to indicate something hasgone wrong

Exceptions are created by being thrown and are detectedby being caught VB uses the keywords Throw Try andCatch to handle these operations

Eg Try and Catch

Private Sub Calculate_Click(helliphelliphelliphelliphellip)Handles CalculateClick

Dim side As Double

Try

side = DoubleParse(txtSideText)

lblDisplayText = ldquoArea is rdquo amp CStr(side side) amp ldquo sq unitsrdquo

Catch exceptionObject As FormatException

lblDisplayText = ldquoError in side re-enterrdquo

End Try

End Sub

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1315FSB23103 13

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Once an exception has been thrown it is possible to provideadditional information by examining the exception objectrsquosMessage and using theToString Method

Eg

bull The message property provides a short string containinga descriptive error message

bull The ToString method returns a string containing severallines providing a trace of the actions that led up to the

exception including the location of where the erroroccurred

Catch exceptionObject As FormatException

MessageBoxShow(exceptionObjectMessage)

MessageBoxShow(exceptionObjectToString())

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1415FSB23103 14

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Eg Error handling

In the case of division by zero by floating-point divisionVBNet outputs the word lsquoinfinityrsquo

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1515FSB23103 15

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

Files and exceptions

File input-output is a major source of exceptions eg

bull incorrect file namebull Disk full

bull CD removed while reading in progress

The FileOpenText method can throw a number of exceptions Inparticular FileNotFoundExceptionEg Program 2 may be modified to include

Catch problem As FileNotFoundException

MessageBoxShow(ldquoError - ldquo amp txtFileText amp _

ldquo file not found Re-enter namerdquo)

Catch problem As Exception

MessageBoxShow(ldquoError ndash concerning file ldquo amp _

txtFileText amp ldquo ldquo amp problemmessage())

End Try

Page 6: Lecture (19)Simple file Exceptions – Error handling

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 615

FSB23103 6

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

StreamWriter Class

The StreamWriter class has two main methods

Write and WriteLineBoth write a string to file but WriteLine adds an end-of-linemarker after the string Write can also be used with noargument to terminate a line with an end-of-line markerEg

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 715

FSB23103 7

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File searching

Searching a file for an item that satisfies some condition isquite commonEg Suppose we have a file of examination marks obtainedby named students

Smith 43 67

Jones 87 99

Patel 54 32Sutton 65 74

etchellip

and we wish to obtain the marks of a particular student fromthe file and display them

Run

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 815

FSB23103 8

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File searching - example

The process may be expressed in structured English as

found = False

While (more lines to read) And found = False

read line

split line into three fields

If first field matches name Thenfound = True

display other fields in labels

End If

End While

If Not found Then

display a warning

End If

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 915

FSB23103 9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Eg File searching - Code

Ensure appropriate input data present

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1015

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1115FSB23103 11

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Error handling - Exceptions

Is designed to deal with run-time errors such as

bull an attempt to divide by zero (integer division)

bull where memory becomes exhausted

bull an out-of-bounds array index

bull arithmetic overflow

Error handling should be used tobull process only exceptional situations

bull recover from infrequent fatal errors

bull safeguard data and exit a program neatly

bull provide some indication of the fault that has arisen

In general program control using conventional controlstructures is more efficient than using error handlingfacilities but the main logic might become obscured

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1215FSB23103 12

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Exception is a term used in VB to indicate something hasgone wrong

Exceptions are created by being thrown and are detectedby being caught VB uses the keywords Throw Try andCatch to handle these operations

Eg Try and Catch

Private Sub Calculate_Click(helliphelliphelliphelliphellip)Handles CalculateClick

Dim side As Double

Try

side = DoubleParse(txtSideText)

lblDisplayText = ldquoArea is rdquo amp CStr(side side) amp ldquo sq unitsrdquo

Catch exceptionObject As FormatException

lblDisplayText = ldquoError in side re-enterrdquo

End Try

End Sub

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1315FSB23103 13

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Once an exception has been thrown it is possible to provideadditional information by examining the exception objectrsquosMessage and using theToString Method

Eg

bull The message property provides a short string containinga descriptive error message

bull The ToString method returns a string containing severallines providing a trace of the actions that led up to the

exception including the location of where the erroroccurred

Catch exceptionObject As FormatException

MessageBoxShow(exceptionObjectMessage)

MessageBoxShow(exceptionObjectToString())

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1415FSB23103 14

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Eg Error handling

In the case of division by zero by floating-point divisionVBNet outputs the word lsquoinfinityrsquo

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1515FSB23103 15

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

Files and exceptions

File input-output is a major source of exceptions eg

bull incorrect file namebull Disk full

bull CD removed while reading in progress

The FileOpenText method can throw a number of exceptions Inparticular FileNotFoundExceptionEg Program 2 may be modified to include

Catch problem As FileNotFoundException

MessageBoxShow(ldquoError - ldquo amp txtFileText amp _

ldquo file not found Re-enter namerdquo)

Catch problem As Exception

MessageBoxShow(ldquoError ndash concerning file ldquo amp _

txtFileText amp ldquo ldquo amp problemmessage())

End Try

Page 7: Lecture (19)Simple file Exceptions – Error handling

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 715

FSB23103 7

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File searching

Searching a file for an item that satisfies some condition isquite commonEg Suppose we have a file of examination marks obtainedby named students

Smith 43 67

Jones 87 99

Patel 54 32Sutton 65 74

etchellip

and we wish to obtain the marks of a particular student fromthe file and display them

Run

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 815

FSB23103 8

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File searching - example

The process may be expressed in structured English as

found = False

While (more lines to read) And found = False

read line

split line into three fields

If first field matches name Thenfound = True

display other fields in labels

End If

End While

If Not found Then

display a warning

End If

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 915

FSB23103 9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Eg File searching - Code

Ensure appropriate input data present

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1015

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1115FSB23103 11

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Error handling - Exceptions

Is designed to deal with run-time errors such as

bull an attempt to divide by zero (integer division)

bull where memory becomes exhausted

bull an out-of-bounds array index

bull arithmetic overflow

Error handling should be used tobull process only exceptional situations

bull recover from infrequent fatal errors

bull safeguard data and exit a program neatly

bull provide some indication of the fault that has arisen

In general program control using conventional controlstructures is more efficient than using error handlingfacilities but the main logic might become obscured

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1215FSB23103 12

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Exception is a term used in VB to indicate something hasgone wrong

Exceptions are created by being thrown and are detectedby being caught VB uses the keywords Throw Try andCatch to handle these operations

Eg Try and Catch

Private Sub Calculate_Click(helliphelliphelliphelliphellip)Handles CalculateClick

Dim side As Double

Try

side = DoubleParse(txtSideText)

lblDisplayText = ldquoArea is rdquo amp CStr(side side) amp ldquo sq unitsrdquo

Catch exceptionObject As FormatException

lblDisplayText = ldquoError in side re-enterrdquo

End Try

End Sub

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1315FSB23103 13

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Once an exception has been thrown it is possible to provideadditional information by examining the exception objectrsquosMessage and using theToString Method

Eg

bull The message property provides a short string containinga descriptive error message

bull The ToString method returns a string containing severallines providing a trace of the actions that led up to the

exception including the location of where the erroroccurred

Catch exceptionObject As FormatException

MessageBoxShow(exceptionObjectMessage)

MessageBoxShow(exceptionObjectToString())

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1415FSB23103 14

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Eg Error handling

In the case of division by zero by floating-point divisionVBNet outputs the word lsquoinfinityrsquo

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1515FSB23103 15

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

Files and exceptions

File input-output is a major source of exceptions eg

bull incorrect file namebull Disk full

bull CD removed while reading in progress

The FileOpenText method can throw a number of exceptions Inparticular FileNotFoundExceptionEg Program 2 may be modified to include

Catch problem As FileNotFoundException

MessageBoxShow(ldquoError - ldquo amp txtFileText amp _

ldquo file not found Re-enter namerdquo)

Catch problem As Exception

MessageBoxShow(ldquoError ndash concerning file ldquo amp _

txtFileText amp ldquo ldquo amp problemmessage())

End Try

Page 8: Lecture (19)Simple file Exceptions – Error handling

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 815

FSB23103 8

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

File searching - example

The process may be expressed in structured English as

found = False

While (more lines to read) And found = False

read line

split line into three fields

If first field matches name Thenfound = True

display other fields in labels

End If

End While

If Not found Then

display a warning

End If

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 915

FSB23103 9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Eg File searching - Code

Ensure appropriate input data present

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1015

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1115FSB23103 11

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Error handling - Exceptions

Is designed to deal with run-time errors such as

bull an attempt to divide by zero (integer division)

bull where memory becomes exhausted

bull an out-of-bounds array index

bull arithmetic overflow

Error handling should be used tobull process only exceptional situations

bull recover from infrequent fatal errors

bull safeguard data and exit a program neatly

bull provide some indication of the fault that has arisen

In general program control using conventional controlstructures is more efficient than using error handlingfacilities but the main logic might become obscured

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1215FSB23103 12

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Exception is a term used in VB to indicate something hasgone wrong

Exceptions are created by being thrown and are detectedby being caught VB uses the keywords Throw Try andCatch to handle these operations

Eg Try and Catch

Private Sub Calculate_Click(helliphelliphelliphelliphellip)Handles CalculateClick

Dim side As Double

Try

side = DoubleParse(txtSideText)

lblDisplayText = ldquoArea is rdquo amp CStr(side side) amp ldquo sq unitsrdquo

Catch exceptionObject As FormatException

lblDisplayText = ldquoError in side re-enterrdquo

End Try

End Sub

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1315FSB23103 13

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Once an exception has been thrown it is possible to provideadditional information by examining the exception objectrsquosMessage and using theToString Method

Eg

bull The message property provides a short string containinga descriptive error message

bull The ToString method returns a string containing severallines providing a trace of the actions that led up to the

exception including the location of where the erroroccurred

Catch exceptionObject As FormatException

MessageBoxShow(exceptionObjectMessage)

MessageBoxShow(exceptionObjectToString())

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1415FSB23103 14

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Eg Error handling

In the case of division by zero by floating-point divisionVBNet outputs the word lsquoinfinityrsquo

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1515FSB23103 15

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

Files and exceptions

File input-output is a major source of exceptions eg

bull incorrect file namebull Disk full

bull CD removed while reading in progress

The FileOpenText method can throw a number of exceptions Inparticular FileNotFoundExceptionEg Program 2 may be modified to include

Catch problem As FileNotFoundException

MessageBoxShow(ldquoError - ldquo amp txtFileText amp _

ldquo file not found Re-enter namerdquo)

Catch problem As Exception

MessageBoxShow(ldquoError ndash concerning file ldquo amp _

txtFileText amp ldquo ldquo amp problemmessage())

End Try

Page 9: Lecture (19)Simple file Exceptions – Error handling

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 915

FSB23103 9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

Eg File searching - Code

Ensure appropriate input data present

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1015

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1115FSB23103 11

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Error handling - Exceptions

Is designed to deal with run-time errors such as

bull an attempt to divide by zero (integer division)

bull where memory becomes exhausted

bull an out-of-bounds array index

bull arithmetic overflow

Error handling should be used tobull process only exceptional situations

bull recover from infrequent fatal errors

bull safeguard data and exit a program neatly

bull provide some indication of the fault that has arisen

In general program control using conventional controlstructures is more efficient than using error handlingfacilities but the main logic might become obscured

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1215FSB23103 12

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Exception is a term used in VB to indicate something hasgone wrong

Exceptions are created by being thrown and are detectedby being caught VB uses the keywords Throw Try andCatch to handle these operations

Eg Try and Catch

Private Sub Calculate_Click(helliphelliphelliphelliphellip)Handles CalculateClick

Dim side As Double

Try

side = DoubleParse(txtSideText)

lblDisplayText = ldquoArea is rdquo amp CStr(side side) amp ldquo sq unitsrdquo

Catch exceptionObject As FormatException

lblDisplayText = ldquoError in side re-enterrdquo

End Try

End Sub

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1315FSB23103 13

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Once an exception has been thrown it is possible to provideadditional information by examining the exception objectrsquosMessage and using theToString Method

Eg

bull The message property provides a short string containinga descriptive error message

bull The ToString method returns a string containing severallines providing a trace of the actions that led up to the

exception including the location of where the erroroccurred

Catch exceptionObject As FormatException

MessageBoxShow(exceptionObjectMessage)

MessageBoxShow(exceptionObjectToString())

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1415FSB23103 14

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Eg Error handling

In the case of division by zero by floating-point divisionVBNet outputs the word lsquoinfinityrsquo

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1515FSB23103 15

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

Files and exceptions

File input-output is a major source of exceptions eg

bull incorrect file namebull Disk full

bull CD removed while reading in progress

The FileOpenText method can throw a number of exceptions Inparticular FileNotFoundExceptionEg Program 2 may be modified to include

Catch problem As FileNotFoundException

MessageBoxShow(ldquoError - ldquo amp txtFileText amp _

ldquo file not found Re-enter namerdquo)

Catch problem As Exception

MessageBoxShow(ldquoError ndash concerning file ldquo amp _

txtFileText amp ldquo ldquo amp problemmessage())

End Try

Page 10: Lecture (19)Simple file Exceptions – Error handling

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1015

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1115FSB23103 11

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Error handling - Exceptions

Is designed to deal with run-time errors such as

bull an attempt to divide by zero (integer division)

bull where memory becomes exhausted

bull an out-of-bounds array index

bull arithmetic overflow

Error handling should be used tobull process only exceptional situations

bull recover from infrequent fatal errors

bull safeguard data and exit a program neatly

bull provide some indication of the fault that has arisen

In general program control using conventional controlstructures is more efficient than using error handlingfacilities but the main logic might become obscured

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1215FSB23103 12

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Exception is a term used in VB to indicate something hasgone wrong

Exceptions are created by being thrown and are detectedby being caught VB uses the keywords Throw Try andCatch to handle these operations

Eg Try and Catch

Private Sub Calculate_Click(helliphelliphelliphelliphellip)Handles CalculateClick

Dim side As Double

Try

side = DoubleParse(txtSideText)

lblDisplayText = ldquoArea is rdquo amp CStr(side side) amp ldquo sq unitsrdquo

Catch exceptionObject As FormatException

lblDisplayText = ldquoError in side re-enterrdquo

End Try

End Sub

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1315FSB23103 13

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Once an exception has been thrown it is possible to provideadditional information by examining the exception objectrsquosMessage and using theToString Method

Eg

bull The message property provides a short string containinga descriptive error message

bull The ToString method returns a string containing severallines providing a trace of the actions that led up to the

exception including the location of where the erroroccurred

Catch exceptionObject As FormatException

MessageBoxShow(exceptionObjectMessage)

MessageBoxShow(exceptionObjectToString())

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1415FSB23103 14

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Eg Error handling

In the case of division by zero by floating-point divisionVBNet outputs the word lsquoinfinityrsquo

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1515FSB23103 15

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

Files and exceptions

File input-output is a major source of exceptions eg

bull incorrect file namebull Disk full

bull CD removed while reading in progress

The FileOpenText method can throw a number of exceptions Inparticular FileNotFoundExceptionEg Program 2 may be modified to include

Catch problem As FileNotFoundException

MessageBoxShow(ldquoError - ldquo amp txtFileText amp _

ldquo file not found Re-enter namerdquo)

Catch problem As Exception

MessageBoxShow(ldquoError ndash concerning file ldquo amp _

txtFileText amp ldquo ldquo amp problemmessage())

End Try

Page 11: Lecture (19)Simple file Exceptions – Error handling

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1115FSB23103 11

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Error handling - Exceptions

Is designed to deal with run-time errors such as

bull an attempt to divide by zero (integer division)

bull where memory becomes exhausted

bull an out-of-bounds array index

bull arithmetic overflow

Error handling should be used tobull process only exceptional situations

bull recover from infrequent fatal errors

bull safeguard data and exit a program neatly

bull provide some indication of the fault that has arisen

In general program control using conventional controlstructures is more efficient than using error handlingfacilities but the main logic might become obscured

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1215FSB23103 12

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Exception is a term used in VB to indicate something hasgone wrong

Exceptions are created by being thrown and are detectedby being caught VB uses the keywords Throw Try andCatch to handle these operations

Eg Try and Catch

Private Sub Calculate_Click(helliphelliphelliphelliphellip)Handles CalculateClick

Dim side As Double

Try

side = DoubleParse(txtSideText)

lblDisplayText = ldquoArea is rdquo amp CStr(side side) amp ldquo sq unitsrdquo

Catch exceptionObject As FormatException

lblDisplayText = ldquoError in side re-enterrdquo

End Try

End Sub

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1315FSB23103 13

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Once an exception has been thrown it is possible to provideadditional information by examining the exception objectrsquosMessage and using theToString Method

Eg

bull The message property provides a short string containinga descriptive error message

bull The ToString method returns a string containing severallines providing a trace of the actions that led up to the

exception including the location of where the erroroccurred

Catch exceptionObject As FormatException

MessageBoxShow(exceptionObjectMessage)

MessageBoxShow(exceptionObjectToString())

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1415FSB23103 14

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Eg Error handling

In the case of division by zero by floating-point divisionVBNet outputs the word lsquoinfinityrsquo

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1515FSB23103 15

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

Files and exceptions

File input-output is a major source of exceptions eg

bull incorrect file namebull Disk full

bull CD removed while reading in progress

The FileOpenText method can throw a number of exceptions Inparticular FileNotFoundExceptionEg Program 2 may be modified to include

Catch problem As FileNotFoundException

MessageBoxShow(ldquoError - ldquo amp txtFileText amp _

ldquo file not found Re-enter namerdquo)

Catch problem As Exception

MessageBoxShow(ldquoError ndash concerning file ldquo amp _

txtFileText amp ldquo ldquo amp problemmessage())

End Try

Page 12: Lecture (19)Simple file Exceptions – Error handling

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1215FSB23103 12

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Exception is a term used in VB to indicate something hasgone wrong

Exceptions are created by being thrown and are detectedby being caught VB uses the keywords Throw Try andCatch to handle these operations

Eg Try and Catch

Private Sub Calculate_Click(helliphelliphelliphelliphellip)Handles CalculateClick

Dim side As Double

Try

side = DoubleParse(txtSideText)

lblDisplayText = ldquoArea is rdquo amp CStr(side side) amp ldquo sq unitsrdquo

Catch exceptionObject As FormatException

lblDisplayText = ldquoError in side re-enterrdquo

End Try

End Sub

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1315FSB23103 13

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Once an exception has been thrown it is possible to provideadditional information by examining the exception objectrsquosMessage and using theToString Method

Eg

bull The message property provides a short string containinga descriptive error message

bull The ToString method returns a string containing severallines providing a trace of the actions that led up to the

exception including the location of where the erroroccurred

Catch exceptionObject As FormatException

MessageBoxShow(exceptionObjectMessage)

MessageBoxShow(exceptionObjectToString())

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1415FSB23103 14

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Eg Error handling

In the case of division by zero by floating-point divisionVBNet outputs the word lsquoinfinityrsquo

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1515FSB23103 15

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

Files and exceptions

File input-output is a major source of exceptions eg

bull incorrect file namebull Disk full

bull CD removed while reading in progress

The FileOpenText method can throw a number of exceptions Inparticular FileNotFoundExceptionEg Program 2 may be modified to include

Catch problem As FileNotFoundException

MessageBoxShow(ldquoError - ldquo amp txtFileText amp _

ldquo file not found Re-enter namerdquo)

Catch problem As Exception

MessageBoxShow(ldquoError ndash concerning file ldquo amp _

txtFileText amp ldquo ldquo amp problemmessage())

End Try

Page 13: Lecture (19)Simple file Exceptions – Error handling

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1315FSB23103 13

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Exceptions ndash contrsquod

Once an exception has been thrown it is possible to provideadditional information by examining the exception objectrsquosMessage and using theToString Method

Eg

bull The message property provides a short string containinga descriptive error message

bull The ToString method returns a string containing severallines providing a trace of the actions that led up to the

exception including the location of where the erroroccurred

Catch exceptionObject As FormatException

MessageBoxShow(exceptionObjectMessage)

MessageBoxShow(exceptionObjectToString())

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1415FSB23103 14

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Eg Error handling

In the case of division by zero by floating-point divisionVBNet outputs the word lsquoinfinityrsquo

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1515FSB23103 15

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

Files and exceptions

File input-output is a major source of exceptions eg

bull incorrect file namebull Disk full

bull CD removed while reading in progress

The FileOpenText method can throw a number of exceptions Inparticular FileNotFoundExceptionEg Program 2 may be modified to include

Catch problem As FileNotFoundException

MessageBoxShow(ldquoError - ldquo amp txtFileText amp _

ldquo file not found Re-enter namerdquo)

Catch problem As Exception

MessageBoxShow(ldquoError ndash concerning file ldquo amp _

txtFileText amp ldquo ldquo amp problemmessage())

End Try

Page 14: Lecture (19)Simple file Exceptions – Error handling

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1415FSB23103 14

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

Eg Error handling

In the case of division by zero by floating-point divisionVBNet outputs the word lsquoinfinityrsquo

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1515FSB23103 15

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

Files and exceptions

File input-output is a major source of exceptions eg

bull incorrect file namebull Disk full

bull CD removed while reading in progress

The FileOpenText method can throw a number of exceptions Inparticular FileNotFoundExceptionEg Program 2 may be modified to include

Catch problem As FileNotFoundException

MessageBoxShow(ldquoError - ldquo amp txtFileText amp _

ldquo file not found Re-enter namerdquo)

Catch problem As Exception

MessageBoxShow(ldquoError ndash concerning file ldquo amp _

txtFileText amp ldquo ldquo amp problemmessage())

End Try

Page 15: Lecture (19)Simple file Exceptions – Error handling

8142019 Lecture (19)Simple file Exceptions ndash Error handling

httpslidepdfcomreaderfulllecture-19simple-file-exceptions-error-handling 1515FSB23103 15

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

Files and exceptions

File input-output is a major source of exceptions eg

bull incorrect file namebull Disk full

bull CD removed while reading in progress

The FileOpenText method can throw a number of exceptions Inparticular FileNotFoundExceptionEg Program 2 may be modified to include

Catch problem As FileNotFoundException

MessageBoxShow(ldquoError - ldquo amp txtFileText amp _

ldquo file not found Re-enter namerdquo)

Catch problem As Exception

MessageBoxShow(ldquoError ndash concerning file ldquo amp _

txtFileText amp ldquo ldquo amp problemmessage())

End Try