Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.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 在这种情况下,如何在if-else语句中抛出异常_Java_Exception - Fatal编程技术网

Java 在这种情况下,如何在if-else语句中抛出异常

Java 在这种情况下,如何在if-else语句中抛出异常,java,exception,Java,Exception,但这也不起作用,我试过一次,但我不知道该抓什么,因为它正进入另一个世界 throw new Exception system.out.println("Not valid range"); 另外,若您像在示例中一样使用检查异常,那个么您必须在方法签名中添加抛出声明 您还可以抛出未经检查的异常(即异常RuntimeException或其扩展),在这种情况下,不需要在方法签名中添加throws 阅读有关异常的更多信息:您在尝试编写时使用了错误的方法 抛出新的异常system.out.println

但这也不起作用,我试过一次,但我不知道该抓什么,因为它正进入另一个世界

throw new Exception system.out.println("Not valid range");
另外,若您像在示例中一样使用检查异常,那个么您必须在方法签名中添加抛出声明

您还可以抛出未经检查的异常(即异常
RuntimeException
或其扩展),在这种情况下,不需要在方法签名中添加
throws


阅读有关异常的更多信息:

您在尝试编写时使用了错误的方法
抛出新的异常system.out.println(“无效范围”),

if(upperCaseString.equals(“X”)| | upperCaseString.equals(“Y”)| | upperCaseString.equals(“MT”))
{
upperCaseString=upperCaseString;
} 
其他的
{
整数整数值;
尝试
{
返回整数.parseInt(大写字符串);
}
catch(NumberFormatException e1)//在这里捕获异常,并根据您的需求抛出您的自定义异常
{
抛出新异常(“无效范围”);
}

如果(integerValueOfsString>=1 | | integerValueOfsString
抛出新异常(“消息”)
异常不是一个代码块。我建议您开始阅读…仅当它是选中的异常时。
throw new Exception system.out.println("Not valid range");
} else {
    throw new Exception("my Message");
}
    if (upperCaseString.equals("X") || upperCaseString.equals("Y") || upperCaseString.equals("MT")) 
    {
        upperCaseString = upperCaseString;
    } 
    else 
    {
        Integer integerValueOfString;
        try
        {
            return  Integer.parseInt(upperCaseString);
        }
        catch (NumberFormatException e1) //Here exception is caught and your custom exception is thrown as per your requirement
        {
            throw new Exception("Not valid range");
        }
        if (integerValueOfString >= 1 || integerValueOfString <= 22) 
        {
            return  integerValueOfString.toString();
        }
        else 
        {
            throw new Exception("Not valid range");
        }
    }