NumberFormatException 发生时如何打印?
How to print when NumberFormatException occurs?
当命令行参数不是整数并且出现 NumberFormatException
时如何打印内容?
我的程序接受 3 个命令行参数并根据它们的内容打印特定文本。
代码如下:
public class CommandLine {
public static void main(String[] args) {
if(args.length !=3){
System.out.println("Error. Must give 3 values");
}
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
int z = Integer.parseInt(args[2]);
if((x%2)==0 && (y%2)==0 &&(z%2)==0)
System.out.println("Even");
else
System.out.println("odd");
}
}
您可以捕获该异常并打印:
int x=y=z=Integer.MIN_VALUE;
try{
x = Integer.parseInt(args[0]);
y = Integer.parseInt(args[1]);
z = Integer.parseInt(args[2]);
}catch (NumberFormatException e) {
System.out.println("x:" +x + " y:" +y +" z:" +z);
e.printStackTrace();
}
仍然是 Integer.MIN_VALUE
的第一个值导致了您的异常(除非您的号码是 Integer.MIN_VALUE
)
if(args.length !=3){
System.out.println("Error. Must give 3 values");
}
else//if the above condition if true so skip these statements
{
try
{
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
int z = Integer.parseInt(args[2]);
if((x%2)==0 && (y%2)==0 &&(z%2)==0)
System.out.println("Even");
else
System.out.println("odd");
}
catch(NumberFormatException ne)
{
System.out.println("Plz! pass only integer values");//catching number format exception
}
}
试试这个
public class CommandLine {
public static void main(String[] args) {
if(args.length !=3){
System.out.println("Error. Must give 3 values");
}
try{
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
int z = Integer.parseInt(args[2]);
if((x%2)==0 && (y%2)==0 &&(z%2)==0)
System.out.println("Even");
else
System.out.println("odd");
}catch(Exception e){
System.out.println("Exception Caught !");
}
}
}
当命令行参数不是整数并且出现 NumberFormatException
时如何打印内容?
我的程序接受 3 个命令行参数并根据它们的内容打印特定文本。
代码如下:
public class CommandLine {
public static void main(String[] args) {
if(args.length !=3){
System.out.println("Error. Must give 3 values");
}
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
int z = Integer.parseInt(args[2]);
if((x%2)==0 && (y%2)==0 &&(z%2)==0)
System.out.println("Even");
else
System.out.println("odd");
}
}
您可以捕获该异常并打印:
int x=y=z=Integer.MIN_VALUE;
try{
x = Integer.parseInt(args[0]);
y = Integer.parseInt(args[1]);
z = Integer.parseInt(args[2]);
}catch (NumberFormatException e) {
System.out.println("x:" +x + " y:" +y +" z:" +z);
e.printStackTrace();
}
仍然是 Integer.MIN_VALUE
的第一个值导致了您的异常(除非您的号码是 Integer.MIN_VALUE
)
if(args.length !=3){
System.out.println("Error. Must give 3 values");
}
else//if the above condition if true so skip these statements
{
try
{
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
int z = Integer.parseInt(args[2]);
if((x%2)==0 && (y%2)==0 &&(z%2)==0)
System.out.println("Even");
else
System.out.println("odd");
}
catch(NumberFormatException ne)
{
System.out.println("Plz! pass only integer values");//catching number format exception
}
}
试试这个
public class CommandLine {
public static void main(String[] args) {
if(args.length !=3){
System.out.println("Error. Must give 3 values");
}
try{
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
int z = Integer.parseInt(args[2]);
if((x%2)==0 && (y%2)==0 &&(z%2)==0)
System.out.println("Even");
else
System.out.println("odd");
}catch(Exception e){
System.out.println("Exception Caught !");
}
}
}