Using Multiple Catch Statements in VB.net
By: Steven Holzner
You also can use multiple Catch statements when you filter exceptions. Here's an example that specifically handles overflow, invalid argument, and argument out of range exceptions:
Module Module1 Sub Main() Dim int1 = 0, int2 = 1, int3 As Integer Try int3 = int2 / int1 System.Console.WriteLine("The answer is {0}", int3) Catch e As System.OverflowException System.Console.WriteLine("Exception: Arithmetic overflow!") Catch e As System.ArgumentException System.Console.WriteLine("Exception: Invalid argument value!") Catch e As System.ArgumentOutOfRangeException System.Console.WriteLine("Exception: Argument out of range!") End Try End Sub End Module
If you want to add a general exception handler to catch any exceptions not filtered, you can add a Catch block for the Exception class at the end of the other Catch blocks:
Module Module1 Sub Main() Dim int1 = 0, int2 = 1, int3 As Integer Try int3 = int2 / int1 System.Console.WriteLine("The answer is {0}", int3) Catch e As System.ArgumentOutOfRangeException System.Console.WriteLine("Exception: Argument out of range!") Catch e As System.ArgumentException System.Console.WriteLine("Exception: Invalid argument value!") Catch e As Exception System.Console.WriteLine("Exception occurred!") End Try End Sub End Module
Archived Comments
1. thank a lot for your site it helps a great deal.
[url=http://www.junjuntex.com/commen
View Tutorial By: ______ ______ at 2017-06-27 10:05:08
2. Much thanks! It is an astounding web page.
[url=http://applebyteme.invisionzone.com/i
View Tutorial By: http://www.elisacavanna.us at 2017-06-23 10:28:21
3. You've gotten one of the better webpages.
[url=http://www.gdaca.com/?option=com_k2&am
View Tutorial By: https://www.commentmaigrir.us at 2017-06-23 08:44:49
4. Much thanks! It a wonderful internet site.
[url=http://casaleao.com/?option=com_k2&am
View Tutorial By: massage lyon croix rousse at 2017-06-22 06:14:43
5. CharlesTig
View Tutorial By: CharlesTig at 2017-06-18 07:51:48
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net
Exception Filtering in the Catch Block in VB.net
Using Multiple Catch Statements in VB.net
Throwing an Exception in VB.net
Throwing a Custom Exception in VB.net
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net