VB.Net/Language Basics/Throw — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:42, 26 мая 2010
Содержание
Any Exception generatesd will be caught in the Catch block that follows
Imports System
Public Class MainClass
Shared Sub Main()
Try
Method1()
" Output String representation of Exception, then output
" values of properties InnerException, Message and StackTrace
Catch exceptionParameter As Exception
Console.WriteLine("exceptionParameter.ToString: " & _
vbCrLf & "{0}" & vbCrLf, exceptionParameter.ToString())
Console.WriteLine("exceptionParameter.Message: " & _
vbCrLf & "{0}" & vbCrLf, exceptionParameter.Message)
Console.WriteLine("exceptionParameter.StackTrace: " & _
vbCrLf & "{0}" & vbCrLf, exceptionParameter.StackTrace)
Console.WriteLine( _
"exceptionParameter.InnerException: " & _
vbCrLf & "{0}" & vbCrLf, _
exceptionParameter.InnerException.ToString())
End Try
End Sub " Main
Public Shared Sub Method1()
Method2()
End Sub
Public Shared Sub Method2()
Method3()
End Sub
Public Shared Sub Method3()
" attempt to convert String to Integer
Try
Convert.ToInt32("Not an integer")
Catch formatException As FormatException
Throw New Exception("Exception occurred in Method3", _
formatException)
End Try
End Sub
End Class
Catch and rethrow exceptions
Imports System
Imports System.Data
Imports System.IO
public class MainClass
Shared Sub Main()
Dim FileToRead As String = "test.txt"
Dim TestFile As FileStream
Dim Reader As New StreamReader(FileToRead)
Do
Dim I As Integer, S As String
Dim Result As Integer
Try
S = Reader.ReadLine()
I = CInt(S)
Result = 100 \ I
Console.WriteLine(Result)
Catch DivByZero As System.DivideByZeroException
Console.WriteLine("** Divide by zero **")
Catch BadConversion As System.InvalidCastException
Console.WriteLine("** " + S + " is not a number **")
Throw New System.InvalidCastException("My own exception happened here", BadConversion)
Catch OtherErrors As Exception
Throw OtherErrors
End Try
Loop While Reader.Peek <> -1
End Sub
End Class
Exception occurs and caught, then rethrown to caller
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Console.WriteLine("Calling ThrowExceptionCatchRethrow")
Try
ThrowExceptionCatchRethrow()
Catch
Console.WriteLine("Caught exception from " & _
"ThrowExceptionCatchRethrow in Main")
End Try
End Sub
Public Shared Sub ThrowExceptionCatchRethrow()
Try
Console.WriteLine("In ThrowExceptionCatchRethrow")
Throw New Exception("Exception in ThrowExceptionCatchRethrow")
Catch exceptionParameter As Exception
Console.WriteLine("Message: " & exceptionParameter.Message)
Throw exceptionParameter
Finally
Console.WriteLine("Finally executed in ThrowException")
End Try
" any code placed here is never reached
Console.WriteLine("End of ThrowExceptionCatchRethrow")
End Sub
End Class
Throw an Exception
Imports System
Imports System.Configuration
Imports System.Resources " Resource readers
Public Class MainClass
Shared Sub Main()
Throw New System.IO.FileLoadException()
End Sub
End Class
Throw an Exception from a Function
Imports System
Imports System.Text
Imports System.Text.RegularExpressions
Public Class MainClass
Shared Sub Main(ByVal args As String())
Console.WriteLine("Enter Run...")
Func1( )
Console.WriteLine("Exit Run...")
End Sub
Shared Public Sub Func1( )
Console.WriteLine("Enter Func1...")
Func2( )
Console.WriteLine("Exit Func1...")
End Sub "Func1
Shared Public Sub Func2( )
Console.WriteLine("Enter Func2...")
Throw New System.Exception( )
Console.WriteLine("Exit Func2...")
End Sub "Func2
End Class
Throw Exception Without Catch
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Try
ThrowExceptionWithoutCatch()
Catch
Console.WriteLine("Caught exception from " & _
"ThrowExceptionWithoutCatch in Main")
End Try
End Sub
Shared Public Sub ThrowExceptionWithoutCatch()
Try
Console.WriteLine("In ThrowExceptionWithoutCatch")
Throw New Exception( _
"Exception in ThrowExceptionWithoutCatch")
Finally
Console.WriteLine("Finally executed in " & _
"ThrowExceptionWithoutCatch")
End Try
" unreachable code; logic error
Console.WriteLine("End")
End Sub
End Class
Throws exception and catches it locally
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
" Try block throws exception
Try
Console.WriteLine("In Throw Exception With Catch")
Throw New Exception("Exception in Throw Exception With Catch")
" catch exception thrown in Try block
Catch exceptionParameter As Exception
Console.WriteLine("Message: " & exceptionParameter.Message)
" Finally executes because corresponding Try executed
Finally
Console.WriteLine( _
"Finally executed in Throw Exception With Catch")
End Try
Console.WriteLine("End")
End Sub
End Class