Java异常处理不返回值
我试图理解如何使用java异常处理在输入无效时仅返回异常消息。据我所知,我必须使用return-out-of-try-catch来编译(或同时使用两者)。但实际上,如果输入参数无效,我不想返回任何内容 如果我处理的是字符串,则会出现null。但这似乎不适用于int 没有办法这样做吗Java异常处理不返回值,java,exception-handling,Java,Exception Handling,我试图理解如何使用java异常处理在输入无效时仅返回异常消息。据我所知,我必须使用return-out-of-try-catch来编译(或同时使用两者)。但实际上,如果输入参数无效,我不想返回任何内容 如果我处理的是字符串,则会出现null。但这似乎不适用于int 没有办法这样做吗 public class Arrays { public static int[] intArray = {1,2,3,4,5,60,7,8,9,10}; public static int arrayGet(
public class Arrays {
public static int[] intArray = {1,2,3,4,5,60,7,8,9,10};
public static int arrayGet(int[] array, int i){
try{
return intArray[i];
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("Please enter number between 0 and "+i);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(arrayGet(intArray,11));
}
}
我的代码没有多大意义,但我想了解如何处理一般情况 您必须返回一些值(必须是int)。对于字符串,您不会遇到这样的问题,因为字符串不是标准数据类型,因此可以为Null。但在这里,您必须在catch语句中返回一些值。您必须返回一些值(必须是int)。对于字符串,您不会遇到这样的问题,因为字符串不是标准数据类型,因此可以为Null。但在这里,您必须在catch语句中返回一些值。目前,您的arrayGet不可编译。如果希望在发生错误时返回字符串,可以在arrayGet的catch块中执行此操作,如
} catch (ArrayIndexOutOfBounds e) {
throw new Exception("my message");
}
主要方法是
try {
int i = arrayGet(11);
} catch (Exception e) {
String msg = e.getMessage();
}
目前您的arrayGet不可编译。如果希望在发生错误时返回字符串,可以在arrayGet的catch块中执行此操作,如
} catch (ArrayIndexOutOfBounds e) {
throw new Exception("my message");
}
主要方法是
try {
int i = arrayGet(11);
} catch (Exception e) {
String msg = e.getMessage();
}
int
和String
的常见超类型是Object
(装箱后)。这意味着您可以返回字符串
或int
,如果您将返回类型声明为对象
:
public static Object arrayGet(int[] array, int i){
try{
return intArray[i];
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("Please enter number between 0 and "+i);
return e.getMessage();
}
}
但是调用者不知道返回了哪种类型,因此他们只能将其作为对象使用
Object o = arrayGet(array, 11);
// o is maybe an int, or maybe a String. But it's definitely an Object.
在这种情况下,方法的参数是罪魁祸首。让调用者知道的一种方法是抛出一个IllegalArgumentException
:
public static int arrayGet(int[] array, int i){
if(i < 0 || i >= array.length)
throw new IllegalArgumentException("Please enter number between 0 and " + i);
return intArray[i];
}
公共静态int数组集(int[]数组,int i){
if(i<0 | | i>=array.length)
抛出新的IllegalArgumentException(“请输入介于0和“+i”之间的数字);
返回阵列[i];
}
int
和String
的常见超类型是对象(装箱后)。这意味着您可以返回字符串
或int
,如果您将返回类型声明为对象
:
public static Object arrayGet(int[] array, int i){
try{
return intArray[i];
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("Please enter number between 0 and "+i);
return e.getMessage();
}
}
但是调用者不知道返回了哪种类型,因此他们只能将其作为对象使用
Object o = arrayGet(array, 11);
// o is maybe an int, or maybe a String. But it's definitely an Object.
在这种情况下,方法的参数是罪魁祸首。让调用者知道的一种方法是抛出一个IllegalArgumentException
:
public static int arrayGet(int[] array, int i){
if(i < 0 || i >= array.length)
throw new IllegalArgumentException("Please enter number between 0 and " + i);
return intArray[i];
}
公共静态int数组集(int[]数组,int i){
if(i<0 | | i>=array.length)
抛出新的IllegalArgumentException(“请输入介于0和“+i”之间的数字);
返回阵列[i];
}
如果成功,则返回数字,否则返回字符串。如果不是这样,请纠正我。我认为,您应该考虑将异常升级到main和catch in main并打印。如果要返回异常消息,为什么要捕获异常?就让打电话的人来处理吧。您可以将它包装在一个选中的异常中并重新抛出,这样您就可以强制调用方处理它。但是,这是一个学生作业,我们被要求提供一个方法的代码,其中有一个边界处理。我是对的,我不能在方法中执行此操作,除非打印出一些消息并让该方法运行到异常。如果成功,您希望返回一个数字,否则返回字符串。如果不是这样,请纠正我。我认为,您应该考虑将异常升级到main和catch in main并打印。如果要返回异常消息,为什么要捕获异常?就让打电话的人来处理吧。您可以将它包装在一个选中的异常中并重新抛出,这样您就可以强制调用方处理它。但是,这是一个学生作业,我们被要求提供一个方法的代码,其中有一个边界处理。我是对的,我不能在一个方法中这样做,除非打印出一些消息并让该方法运行到异常中。我必须稍微更改方法声明public static int arrayGet(int[]array,int I)抛出异常
,您的代码对我来说运行良好。thxI必须稍微更改方法声明公共静态int-arrayGet(int[]array,inti)抛出异常
,您的代码对我来说运行良好。谢谢