您好我想知道相同方法的两种变体之间的区别和相似之处是什么。
public string test(String value)
throw new testException();
和
public abstract String test(String value) throw new testException;
如果我的语法错误,请原谅我。
答案 0 :(得分:3)
public abstract String test(String value) throw new testException;
没有意义。你最接近的是写
public abstract String test(String value) throws testException;
表示test
是可以抛出testException
的方法。如果testException
不是RuntimeException
,那么必须这样声明。但是在方法签名中添加throws testException
只表示方法可以抛出该异常,它实际上并没有进行抛出。
答案 1 :(得分:1)
在java中,每个方法都应该使用以下语法指示它们是否抛出异常:
Option Strict Off
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms
Imports System.Drawing
Imports System.IO
Public Class Form1
Dim thisConnection = New SqlConnection("Server=myserver;Database=myDatabase;User Id=username;Password=password")
Dim DBCommand As SqlCommand
Dim myReader As SqlDataReader
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
thisConnection.Open()
Console.WriteLine("Connect Open")
DBCommand.Parameters.AddWithValue("@dateOld", "20151215")
DBCommand.Parameters.AddWithValue("@dateNew", "20151231")
Dim myReader = DBCommand.ExecuteReader()
Dim fNextResult As Boolean = True
Dim fileName As String = "C:\Users\Documents\MomInt.txt"
DBCommand = New SqlCommand("*****SQL code*****")
Dim outputStream As StreamWriter = New StreamWriter(fileName)
'Get all values from the current item on the reader as long asRead() returns true...
Do While myReader.Read
'make an array the length of the available fields
Dim values(myReader.FieldCount - 1) As Object
'get all the field values
myReader.GetValues(values)
'write the text version of each value to a comma seperated string
Dim line As String = String.Join(",", values)
'write the csv line to the file
outputStream.WriteLine(line)
Loop
myReader.Close()
outputStream.Close()
Me.Text = "Success!"
Me.BackColor = Color.Chartreuse()
thisConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Connection Failed!", MessageBoxButtons.AbortRetryIgnore)
End Try
End Sub
End Class
如果使用抽象方法,方法签名只需要包含public String test(String value) throws testException
{
//your code here
//at some point you will throw the exception like this:
throw new testException();
}
表达式,但是当您实现该方法时,需要在方法体中添加throw语句。