📄 form1.vb
字号:
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports CodeForChapter4vb_DLL.CodeForChapter4cs_DLL
Imports System.Diagnostics
Imports System.IO
Imports System.Collections
Imports System.Reflection
Namespace CodeForChapter4cs
Public Partial Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
' bool b;
Try
Catch e1 As Exception
'ex.StackTrace
Throw
End Try
Dim s As String = "start"
For i As Integer = 0 To 29
s &= "let's kill our perf"
Next i
Me.ReturnSomething()
End Sub
Public Function ReturnSomething() As Integer
Return 5
End Function
Private Sub menuItem1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles menuItem1.Click
'this.SomeMethod();
Me.AnotherMethod(New SomeClass())
End Sub
Private Sub SomeMethod()
' 1. let any exception bubble up
Me.MethodThatMayThrow()
' 2. rethrow
Try
Me.MethodThatMayThrow()
Catch e1 As Exception
' do something with the exception object
' or do something else as cleanup in this code block
Throw 'rethrows
End Try
' 3. Catch exception
Try
Me.MethodThatMayThrow()
Catch e2 As Exception
' FULLY deal with this exception
'ex.st
End Try
' 4. Swallow
Try
Me.MethodThatMayThrow()
Catch e3 As Exception
' swallow, just so it doesn't go up to other methods
' DO NOT do this!
End Try
End Sub
Private Sub MethodThatMayThrow()
Throw New Exception("The method or operation is not implemented.")
End Sub
Private Sub AnotherMethod(ByVal obj As SomeClass)
Try
CType(obj, ISomeInterface).DoIt()
Catch ex As InvalidCastException
' log the exception to file
Debug.WriteLine("The stacktrace: " & Constants.vbCrLf & ex.StackTrace)
' take some real recovery action!
End Try
End Sub
Private Sub menuItem2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles menuItem2.Click
Dim obj As SomeType = Nothing
obj.SomeMethod() ' NullReferenceException
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
'this.DoSomethinginterestingWith(@"\testFile.txt");
Dim ar As ArrayList=Nothing
'ar.
ar.Capacity = 1
Me.WorkWithSomeList(ar)
End Sub
Private Sub DoSomethinginterestingWith(ByVal path As String)
Try
Dim fs As FileStream = File.Open(path, FileMode.Open) 'FileNotFoundException
' do something interesting with fs
Catch ex As FileNotFoundException
' TODO deal with the invalid input
End Try
End Sub
'private void DoSomethinginterestingWith(string path)
'{
' if (File.Exists(path))
' {
' FileStream fs = File.Open(path, FileMode.Open); //FileNotFoundException
' // do something interesting with fs
' }
' else
' {
' // TODO deal with the invalid input
' }
'}
'private void AnotherMethod(SomeClass obj)
'{
' ISomeInterface i = obj as ISomeInterface;
' if (i != null)
' {
' ((ISomeInterface)obj).DoIt();
' }
' else
' {
' // Do something else!
' }
'}
Private Sub WorkWithSomeList(ByVal ar As ArrayList)
ar.Add(New Object())
If Not(ar.IsFixedSize OrElse ar.IsReadOnly) Then
Else
' do some sensible alternative
End If
End Sub
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button2.Click
Dim t As Type = Me.GetType()
Dim m As MethodInfo = t.GetMethod("DoIt")
m.Invoke(Me, Nothing)
End Sub
Public Sub DoIt()
Throw New InvalidOperationException("my msg")
End Sub
End Class
Public Class SomeType
Public Sub SomeMethod()
End Sub
End Class
End Namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -