Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我应该什么时候宣布;抛出“;,什么时候没有?_Java_Exception_Try Catch_Throw - Fatal编程技术网

Java 我应该什么时候宣布;抛出“;,什么时候没有?

Java 我应该什么时候宣布;抛出“;,什么时候没有?,java,exception,try-catch,throw,Java,Exception,Try Catch,Throw,例如,我注意到这两种方法都有效: public void func(int a) throws IllegalArgumentException { if(a < 0) throw new IllegalArgumentException("a should be greater than 0."); } public void func(int a) { if(a < 0) throw new IllegalArgumentEx

例如,我注意到这两种方法都有效:

public void func(int a) throws IllegalArgumentException {
    if(a < 0) 
        throw new IllegalArgumentException("a should be greater than 0.");
}

public void func(int a) {
    if(a < 0) 
        throw new IllegalArgumentException("a should be greater than 0.");
}
public void func(int a)抛出IllegalArgumentException{
if(a<0)
抛出新的IllegalArgumentException(“a应该大于0”);
}
公共无效函数(int a){
if(a<0)
抛出新的IllegalArgumentException(“a应该大于0”);
}
我不禁要问:

我应该在什么时候宣布
抛出异常,什么时候不宣布,只是在没有声明的情况下抛出异常?

来自:

Java编程语言要求程序包含 执行 方法或构造函数。对于每个检查的异常,这是一个可能的 结果,方法(§8.4.6)或构造函数的抛出子句 (§8.8.5)必须提及该例外情况的类别或 该异常类的超类(§11.2.3)

因此,简单地说,如果检查到异常,则需要使用
throws
语句。如果您有一个
异常
,并且它不是
运行时异常
的子类,那么将选中它


IllegalArgumentException
RuntimeException
的子类,因此它是未选中的,您不需要在throws语句中声明它。对于大多数这样的异常(
IllegalArgumentException
NullPtrException
等),这是正确的,因为不能合理地期望您轻松处理这些异常。

检查的异常必须始终写入它抛出的方法签名中

如果出现异常,您不必在方法名称中写入
throws
,但强烈建议这样做,因为它更清晰,并且会在方法的文档中提到

/**
 * @throws This won't appear if you don't write `throws` and this might
 * mislead the programmer.
 */
public void func(int a) throws IllegalArgumentException {
    if(a < 0) 
        throw new IllegalArgumentException("a should be greater than 0.");
}
/**
*@throws如果你不写'throws',这个不会出现,这可能
*误导程序员。
*/
public void func(int a)引发IllegalArgumentException{
if(a<0)
抛出新的IllegalArgumentException(“a应该大于0”);
}

当检查异常时,编译器要求您捕获异常或宣布抛出声明。

检查post以比较检查的异常和未检查的异常。

抛出的美妙之处在于,异常会转换为检查的异常,因此,每当任何开发人员尝试使用第一种方法时,都会警告他在调用已在其签名中抛出的方法之前放置一个try-catch块。

这是因为
IllegalArgumentException扩展了RuntimeException
。你不能像这样把throws子句放在下面

public void func(int a) {
    if(a == 0) 
        throw new Exception("a should be greater than 0.");
}

有许多技术答案是JLS第1.2节,检查异常等,解释了它的使用。
但是你的问题

我应该在什么时候宣布抛出异常,什么时候不宣布,只是在没有声明的情况下抛出异常

如果您的代码正在生成某个显式异常并抛出它,那么应该始终添加throws子句,并且您应该只需在将为您生成文档标记的函数上方写入/**即可生成文档这将帮助所有使用您的库或函数的其他用户如果在调用此函数之前未初始化某些无效参数或值,则特定函数或构造函数必定会引发异常

示例

/**
 * 
 * @param filePath File path for the file which is to be read   
 * @throws FileNotFoundException In case there exists no file at specified location
 * @throws IllegalArgumentException In case when supplied string is null or whitespace
 */
public static void ReadFile(String filePath) throws FileNotFoundException, IllegalArgumentException 
{
    if(filePath == null || filePath == "")
     throw new IllegalArgumentException(" Invalid arguments are supplied ");
    File fil = new File(filePath);
    if(!fil.exists()|| fil.isDirectory())
        throw new FileNotFoundException(" No file exist at specified location " + filePath);
    //..... Rest of code to read file and perform necessay operation
}
此外,这些文档标记和抛出是为了使程序员的生活更轻松,他们将重用您的代码,并提前知道,如果使用错误的参数调用函数或指定位置的文件不可用,将抛出IllegalArgumentException、FileNotFoundException

因此,如果您希望您的代码和函数是自解释的,并提供所有必要的情况,即抛出什么异常,则添加以下子句,否则这是您的选择。

请记住,如果30天后您自己正在使用(我相信您将在不久的将来重新使用它)这些函数,那么它将更有帮助,您将通过这些子句确切地知道我在该函数中做了什么。

当您需要声明选中的异常时, 未检查的异常如果方法或构造函数的执行可以抛出异常,则不需要在方法或构造函数的throws子句中声明异常。。从-

No,不建议使用有效的Java在
throws
中编写未经检查的异常。