♥ BÌNH LUẬN MỚI NHẤT ♥



Xem Thêm

Tắt Thông Báo.

How to VB.NET Exceptions

Exceptions are the occurrence of some condition that changes the normal flow of execution . For ex: you programme run out of memory , file does not exist in the given path , network connections are dropped etc. More specifically for better understanding , we can say it as Runtime Errors .
In .NET languages , Structured Exceptions handling is a fundamental part of Common Language Runtime . It has a number of advantages over the On Error statements provided in previous versions of Visual Basic . All exceptions in the Common Language Runtime are derived from a single base class , also you can create your own custom Exception classes. You can create an Exception class that inherits from Exception class .
You can handle Exceptions using Try..Catch statement.
Try
code
exit from Try
Catch [Exception [As Type]]
code - if the exception occurred this code will execute exit from Catch

Finally
The code in the finally block will execute even if there is no Exceptions. That means if you write a finally block , the code should execute after the execution of try block or catch block.
Try
code
exit from Try
Catch [Exception [As Type]]
code - if the exception occurred this code will execute
exit Catch
Finally
code - this code should execute , if exception occurred or not

The following example trying to divide a number by zero.
Try
Dim i As Integer
Dim resultValue As Integer
i = 100
resultValue = i / 0
MsgBox("The result is " & resultValue)
Catch ex As Exception
MsgBox("Exception catch here ..")
Finally
MsgBox("Finally block executed ")
End Try

How to throw Exception
You should throw exceptions only when an unexpected or invalid activity occurs that prevents a method from completing its normal function. You can throw any type of Throwable object using the keyword throw. . More about.... throw exception

How to create a custom exception
We can create our own exceptions by extending 'Exception' class. It will simplify and improve the error handling and thus increase the overall code quality. More about.... Create a custom exception

System level Exceptions Vs Application level Exceptions
Exceptions are provide a structured, uniform, and type-safe way of controlling both system level and application level abnormal conditions. Application exceptions can be user defined exceptions thrown by the application. System exceptions are common exceptions thrown by the CLR. More about.... System level Exceptions VS ..

NullReferenceException
A NullReferenceException exception is thrown when you try to access a member on a type whose value is null. That means the reference to an Object which is not initialized. More about....

NullReferenceException
Difference between Exception and Error
Exceptions are related to the application and an Error is related to the environment in which the application is running. More about.... Exception and Error
From the following VB.NET code , you can understand how to use try..catch statements. Here we are going to divide a number by zero .
Example :
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim i As Integer
Dim resultValue As Integer
i = 100
resultValue = i / 0
MsgBox("The result is " & resultValue)
Catch ex As Exception
MsgBox("Exception catch here ..")
Finally
MsgBox("Finally block executed ")
End Try
End Sub
End Class

When you execute this program you will "Exception catch here .." first and then "Finally block executed " . That is when you execute this code , an exception happen and it will go to catch block and then it will go to finally block.

Không có nhận xét nào:

Đăng nhận xét

FOLLOWERS

TỔNG LƯỚT TRUY CẬP