0% found this document useful (0 votes)
10 views

Programming Part 8

Programming

Uploaded by

niftyrue
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Programming Part 8

Programming

Uploaded by

niftyrue
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ERROR HANDLING FUNDAMENTALS

VANE SHUNGU VACHAFARA

Name:Chibi Tinodaishe
Phone:0781081816
Email:[email protected]
Address:Mutare
WHAT ARE ERRORS ?
Errors: Unexpected events that disrupt program execution.

WHAT IS EXCPETION HANDLING?


Exception Handling: A structured approach to catching and managing errors

Key Concepts in VB.NET

Try...Catch Block:

 Try block: Contains code that might throw an exception.


 Catch block(s): Executed when an exception is thrown within the Try
block.
 Catch statement: Specifies the type of exception to handle.
 Exception Type: The specific exception class
(e.g., System.FormatException).
 Variable Name: To store the exception object (optional).
 Catch block body: Code to handle the exception (e.g., display an error
message, log the error, perform cleanup).
Finally Block:

 Optional block that always executes, regardless of whether an exception


is thrown.
 Used for cleanup tasks like closing files or releasing resources.

Example:
Module ErrorHandlingExample

Sub Main()
Dim numberString As String = "hello"
Try
Dim number As Integer = Integer.Parse(numberString) ' Might
throw FormatException
Console.WriteLine("Number: {0}", number)
Catch ex As FormatException
Console.WriteLine("Error: Invalid number format.")
' Log the error or perform other actions
Finally
Console.WriteLine("Program execution completed.")
End Try
End Sub

End Module
Explanation
1. Try block attempts to parse the string numberString as an integer.
2. If parsing fails (FormatException is thrown), the Catch block executes.
3. The Catch block displays an error message and you can optionally store
the exception object in ex.
4. The Finally block always prints a message, regardless of errors.

Common Exception Types:


 System.FormatException: Thrown when an attempt to convert a value to
a different data type fails (e.g., parsing invalid string to integer).
 System.NullReferenceException: Thrown when a variable is used before
being assigned a value or if it refers to Nothing.
 System.IO.FileNotFoundException: Thrown when a file cannot be
found during file operations.
 System.DivideByZeroException: Thrown when attempting to divide by
zero.
Tips for A-Level Candidates:
 Use Try...Catch blocks when working with code that might throw
exceptions.
 Handle specific exception types for targeted error messages and
actions.
 Use Finally blocks for essential cleanup tasks.
 Test your code thoroughly with different inputs to ensure proper
error handling.

Additional Notes:
 Consider using the Throw statement to explicitly throw exceptions
within your code.
 VB.NET also supports nested Try...Catch blocks for more complex
error handling scenarios.
 Explore built-in error handling mechanisms in libraries you use (e.g.,
file I/O).

You might also like