Exception filters are way to restrict catch blocks with conditions.
TryExceptions filters are created with When -keyword and condition is specified after it.
MethodThatWillCrash()
Catch ex As Exception When DateTime.Now.DayOfWeek = DayOfWeek.Monday
MessageBox.Show("Exceptions are caught on mondays!")
End Try
This leads us to proper use case.
Catching multiple exceptions in one catch block
Private Sub SomeMethod()This code will not show messagebox when code is run, because ArgumentException catch block is empty. We could fix it by changing the catch block to check exception type:
Try
MethodThatWillCrash()
Catch argException As ArgumentException
Catch invalidCastException As InvalidCastException
MessageBox.Show("Exception!")
End Try
End Sub
Private Sub MethodThatWillCrash()
Throw New ArgumentException()
End Sub
TryBut the Else statement is dull and catch statement is kind of unclear to read because it catches all exceptions.
MethodThatWillCrash()
Catch generalException As Exception
If TypeOf generalException Is ArgumentException OrElse TypeOf generalException Is InvalidCastException Then
MessageBox.Show("Exception!")
Else
Throw
End If
End Try
TryWith When -keyword we can specify types of exception to catch. Neat!
MethodThatWillCrash()
Catch generalException As Exception When _
TypeOf generalException Is ArgumentException OrElse
TypeOf generalException Is InvalidCastException
MessageBox.Show("Exception!")
End Try
Exception filters posses great strengths, but beware, they can cause some unexpected behaviors: https://blogs.msdn.microsoft.com/dotnet/2009/08/25/the-good-and-the-bad-of-exception-filters/
Btw. exception filters are coming also to C# 6!
http://geekswithblogs.net/BlackRabbitCoder/archive/2015/04/09/c.net-little-wonders-exception-filtering-in-c-6.aspx
Given article is very helpful and very useful for my admin, and pardon me permission to share articles here hopefully helped:
ReplyDeleteErp In Chennai
IT Infrastructure Services Software
ERP software company
web design company in india
Remote Infrastructure Services Software
College Management System Software
School Management System Software
Cloud Erp Software Company In India
Best web development company
ERP in India
Excellent information with unique content and it is very useful to know about the information based on blogs.
ReplyDeleteConstruction company in aranthangi pudukottai
best Construction company
cement wholesale dealers in aranthangi pudukkottai
TMT steel bars wholesale dealers in aranthangi
best interior designers
construction resources and equipments providers
best hardware product suppliers
electrical products and services
I really appreciate this amazing post which you have provided for us. I assure that it will be beneficial for most people.
ReplyDeleteCustomer Relationship Management Software
hospital management system
erp for trading
web design company in india
Remote Infrastructure Services Software
College Management System Software
School Management System Software
erp billing software
construction management system
ERP in India
hotel management system
HR management system
online booking system
Nice Post Thanks For Sharing This Post
ReplyDelete3iSchools Provide Best School ERP Software Company in India,Best School Management Software Company in India,Best School Administration Software Company in India.
Nice Post Thanks For Sharing This Post
ReplyDeleteBest School Management Software Company in India