看看下面的
代码,怎么没有
错误提示呀!!!!!! 自己建的类: Private name As String Public Property nm() As String Get Return Me.name End Get Set(ByVal value As String) Try If value <= 10 And value > = 1 Then Me.name = value End If Catch ex As Exception Message
Box.Show(ex.Message) End Try End Set End Property 按钮为4: Private Sub Button4_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button4.Click Dim nam As New Class1 nam.nm = 1333 Message
Box.Show(nam.nm) End Sub __________________________________________________________________________ Public Class Class1 Private name As String Public Property nm() As String Get Return Me.name End Get Set(ByVal value As String) Try If value <= 10 And value > = 1 Then Me.name = value Else Me.name = "Error " Throw New Exception( "The value is out of range. ") End If Catch ex As Exception Message
Box.Show(ex.Message) End Try End Set End Property End Class __________________________________________________________________________ Set(ByVal value As String) Try If value <= 10 And value > = 1 Then 到底是string还是int类型? 如果是string要用 len()求哦。 __________________________________________________________________________ 楼主
错误理解了Try的使用
方法,如果没有出系统级别
错误Try是不会报错的 应将如下
代码进行
修改: Try If value <= 10 And value > = 1 Then Me.name = value End If Catch ex As Exception Message
Box.Show(ex.Message) End Try 改后
代码: If value <= 10 And value > = 1 Then Me.name = value Else Msg
Box(Value) End If __________________________________________________________________________ VB.NET默认允许隐式转换 value 会
自动转换为Double类型 楼主是不是希望检测字符串长度,不太明白,给出两种方式,尽量不要用Try做逻辑验证 If value Is Nothing Then Msg
Box( "传入的值为空! ") : Exit Property If value.Length > 0 AndAlso value.Length < 11 Then Me.name = value Else Msg
Box( "传入的值溢出! ") End If If IsNumeric(value) = True Then If CInt(value) > 0 AndAlso CInt(value) < 11 Then Me.name = value Else Msg
Box( "传入的值溢出! ") End If Else Msg
Box( "传入的值不是数字类型! ") End If __________________________________________________________________________ 好像是问题问的有点不太清楚,在这里补充一下!!! 进行
错误提示,但是当我将nam.nm = 1333时,系统却没有
提示我出错 我规定的范围时 从 1 到 10 超出范围了但是没有
提示我出错,这怎么
解决呀!!!!! __________________________________________________________________________ 超出了范围如果想明确的引发
自定义的异常,必须自己Throw出来一个异常 就像楼上各位举的例子一样.. __________________________________________________________________________ 那用 try 里的 message
Box.show(ex.message) 不行吗??? 还是只能用throw 呀!!!!! __________________________________________________________________________ 是不是这样最简单: Try If cint(value) <= 10 And cint(value) > = 1 Then Me.name = value End If Catch ex As Exception Throw New Exception(ex.Message) End Try __________________________________________________________________________ 这样也不会抛出
错误吧? __________________________________________________________________________ Private name As String Public Property nm() As String Get Return Me.name End Get Set(ByVal value As String) If value <= 10 And value > = 1 Then Me.name = value Else ‘抛出
自定义的异常咯... End If End Set End Property __________________________________________________________________________ Public Property nm() As String Get Return Me.name End Get Set(ByVal value As String) If CInt(val(value)) <= 10 And CInt(val(value)) > = 1 Then Me.name = value Else ‘抛出
自定义的异常咯... End If End Set End Property __________________________________________________________________________ 根本没有发生异常,怎么又会报错呢 __________________________________________________________________________ 当你
代码出现异常时才会报错! __________________________________________________________________________ Private name As String Public Property nm() As String Get Return Me.name End Get Set(ByVal value As String) Try If value <= 10 And value > = 1 Then Me.name = value else Catch ex As Exception Message
Box.Show(ex.Message) EndIf End Try End Set End Property __________________________________________________________________________
原文链接:https://www.f2er.com/vb/263752.html