SDLC
Competitive Programming for Beginners: Topcoder Challenges
What is Competitive Programming? Competitive programming is solving coding problems using...
An exception refers to a problem that arises during program execution. It is brought about by an unexpected circumstance. A good example is when you are performing a division operation, and then you divide by zero (0). An exception will be raised.
In this Visual Basic Tutorial, you will learn:
With exceptions, you can transfer the control of a program from one part to another. In VB.NET, exceptions are handled using the following 4 keywords:
| Keyword | Detail |
| Try | The work of the Try block is to identify the code block for which a specific exception will be activated. It should be followed by a catch block(s). |
| Catch | Catching of the Exception is done in this block. It is an exception handler in which the Exception is handled. |
| Finally | Use the Finally block to run a set of statements whether an exception has occurred or not. |
| Throw | An exception is thrown after the occurrence of a problem. This is the work of the Throw keyword. |
The Try/Catch statements take the syntax given below:
Try [ try_Statement(s) ] [ Exit Try ] [ Catch [ exception_name [ As type ] ] [ When expression ] [ catch_Statement(s) ] [ Exit Try ] ] [ Catch ... ] [ Finally [ finally_Statement(s) ] ] End Try
The Try/Catch block should surround the code that may raise an exception. This code is known as a protected code. You can use multiple catch statements when you need to catch various types of exceptions.
With the Try/Catch statements, you can separate your ordinary program code from the error-handling System. Let us demonstrate how to handle an exception using the Try, Catch, and Finally keywords.
Step 1) Create a new console application.
Step 2) Use this code:
Module Module1
Sub divisionFunction(ByVal n1 As Integer, ByVal n2 As Integer)
Dim answer As Integer
Try
answer = n1 \ n2
Catch ex As DivideByZeroException
Console.WriteLine("Exception: {0}", ex)
Finally
Console.WriteLine("Answer is: {0}", answer)
End Try
End Sub
Sub Main()
divisionFunction(4, 0)
Console.ReadKey()
End Sub
End Module
Step 3) Click the Start button from the toolbar to execute the code. You should get the following output:
Here is a screenshot of the code:
Explanation of Code:
VB.NET allows you to define your own exceptions. You can get user-defined exception classes from ApplicationException class. Let us demonstrate this by an example:
Step 1) Create a new console application.
Step 2) Use the following code:
Module Module1
Public Class HeightIsZeroException : Inherits ApplicationException
Public Sub New(ByVal text As String)
MyBase.New(text)
End Sub
End Class
Public Class Height
Dim height As Integer = 0
Sub showHeight()
If (height = 0) Then
Throw (New HeightIsZeroException("Zero Height found"))
Else
Console.WriteLine("Height is: {0}", height)
End If
End Sub
End Class
Sub Main()
Dim hght As Height = New Height()
Try
hght.showHeight()
Catch ex As HeightIsZeroException
Console.WriteLine("HeightIsZeroException: {0}", ex.Message)
End Try
Console.ReadKey()
End Sub
End Module
Step 3) Click the Start button from the top bar to execute the code. You should get the following output:
Here is a screenshot of the code:
Explanation of Code:
In exception handling, you can choose to throw an object. However, the object must be derived from System. Exceptionclass, directly or indirectly. For example:
Step 1) Create a new console application.
Step 2) Use the following code:
Module Module1
Sub Main()
Try
Throw New ApplicationException("Throwing a custom exception")
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
Console.WriteLine("The Finally Block")
End Try
Console.ReadKey()
End Sub
End Module
Step 3) Click the Start button from the top bar to execute the code. You should get the following:
Here is a screenshot of the code:
Explanation of Code:
What is Competitive Programming? Competitive programming is solving coding problems using...
Ansible is a DevOps tool which automates software provisioning, configuration management, and...
$20.20 $9.99 for today 4.5 (114 ratings) Key Highlights of C# Tutorial PDF 243+ pages eBook...
Before learning about the Dark web and Deep web, let us learn first about the surface web. What is...
A Partition is a hard drive section that is separated from other parts. It enables you to divide...
A GPU benchmark is a test that helps you to compare the speed, performance, and efficiency of the...