所以我一般有两个关于java的一般性问题.第一个是在方法体中何时使用try/catch而在声明方法时使用throws异常?这是我的意思的一点演示.这个:
public void whileChatting() throws IOException{}
Run Code Online (Sandbox Code Playgroud)
与
public void closeConnection() {
try {
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
然后我的第二个问题是什么时候才能知道捕获或抛出什么类型的异常?我的意思是IOException或EOFException等异常......
如果有一个很好的联系,有人可以让我教这一切(因为它可能比我想的更复杂)我会像你回答的那样感激.谢谢.
在 swift 中使用 @dynamicMemberLookup 时,subscript无法声明“抛出”。
subscript(dynamicMember member: String) -> Any
Run Code Online (Sandbox Code Playgroud)
还行吧。
subscript(dynamicMember member: String) throws -> Any
Run Code Online (Sandbox Code Playgroud)
这会产生编译错误。
我最近发现可以在 Javadoc 中对同一个异常使用多个@throws标签。
我的一位学生用它来记录他在《四子棋》中的一种方法:
/*
* ...
* @throws IllegalArgumentException if the number of rows or columns is invalid
* @throws IllegalArgumentException if one of the players has {@link Stone#None} as stone
* @throws IllegalStateException if both players use the same stone color
*/
public void doSomething(...) { ... }
Run Code Online (Sandbox Code Playgroud)
现在我(和他)的问题是:对于是否使用单个@throws标签或“是否可以”为每个异常类型使用多个标签,是否有官方风格指南或一般建议?
@throws如果我的应用程序出于多种原因抛出相同的异常,我可以使用多个javadoc标记吗?例如:
@throws UserException if issue 1 happened
@throws UserException if issue 2 happened
@throws UserException if issue 3 happened
Run Code Online (Sandbox Code Playgroud)
是否被JavaDoc标准禁止?
我有以下课程
public abstract interface X
{
public abstract void f() throws java.io.IOException;
}
public class Y implements X
{
public void f() throws java.io.IOException
{
throw new java.ioIOException("Hello");
}
public static void main(String [] args)
{
X x = new Y();
try
{
x.f();
}
catch (IOException e)
{
System.out.println("Caught");
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我编译两个并得到X.class和Y.class.
现在我改变X来移除投掷
public abstract interface X
{
public abstract void f();
}
Run Code Online (Sandbox Code Playgroud)
显然,如果我重新编译X和Y,Y的编译将失败
Y.java:4: f() in Y cannot implement f() in X; overridden …Run Code Online (Sandbox Code Playgroud) 我不知道为什么IllegalArgumentException不类需要被逮住或声明,而其他的异常有(例如java.net.MalformedURLException)。
public void foo() {
throw new IllegalArgumentException("spam");
}
public void bar() throws MalformedURLException { // required
throw new MalformedURLException("ham");
}
Run Code Online (Sandbox Code Playgroud)
我知道Errors 不必声明,因为它们不打算被捕获。
我想声明一个新的异常,它也不需要被捕获。
在Java中,throws如果原始抽象方法不能(overridden method does not throw Exception.),则无法指定重写的抽象方法是否有异常.但是在Scala中,您可以执行此操作,因为它没有检查异常.很好,但如果您使用的@throws注释应该将Java编译器发送给正在发生的事情,对吧?
鉴于此Scala代码:
package myscala
abstract class SFoo {
def bar(): Unit
}
class SFoobar extends SFoo {
@throws[Exception]
override def bar(): Unit = {
throw new Exception("hi there")
}
}
Run Code Online (Sandbox Code Playgroud)
我有两个不同的Java程序,其中一个将Exception在运行时编译并运行,另一个将无法编译.
编译:
import myscala.SFoo;
import myscala.SFoobar;
public class Foobar {
public static void main(String[] args) {
SFoo mySFoo = new SFoobar();
mySFoo.bar();
}
}
Run Code Online (Sandbox Code Playgroud)
不编译(unreported exception Exception; must be caught or declared to be …
我有这样的代码Java,我用来报告异常(抛出FileNotFoundException,IOException,ClassNotFoundException).
例:
private void functionName() throws FileNotFoundException, IOException, ClassNotFoundException{}
Run Code Online (Sandbox Code Playgroud)
我需要这样做C#,我该怎么做?
此代码未在Swift 3.3上编译.它显示了消息:'self'在super.init调用可以访问的'catch'块中使用
public class MyRegex : NSRegularExpression {
public init(pattern: String) {
do {
try super.init(pattern: pattern)
} catch {
print("error parsing pattern")
}
}
}
Run Code Online (Sandbox Code Playgroud)
那可能是什么?
我想定义一个界面,比如
public interface Visitor <ArgType, ResultType, SelfDefinedException> {
public ResultType visitProgram(Program prog, ArgType arg) throws SelfDefinedException;
//...
}
Run Code Online (Sandbox Code Playgroud)
在实现过程中,selfDefinedException会有所不同.(selfDefinedException为现在的通用undefined)有没有办法做到这一点?
谢谢