Java中的异常,打印一个列表。

huangapple go评论71阅读模式
英文:

Exception in java, print a list

问题

我有一个关于异常的一般性问题
我应该写什么代码才能打印出整个列表
在输出末尾的时候我的输出以 **"String is null"** 结束
<br>谢谢




	public static void main(String[] args) {

		List<String> list = new ArrayList<String>();
		Collections.addAll(list, "hi", null, "hello");

		try {
			print(list);
		} catch (NullPointerException e) {
		System.out.println(e.getMessage());
		}
	}

	private static void print(List<String> list) {

		for (String string : list) {

			if (string == null) {
				throw new NullPointerException("String is null");
			}

			System.out.println(string);

		}
	}
英文:

I have a general question about exception.
What should I write so that the whole list is printed?
At the time my output ends by "String is null"
<br>Thank you

public static void main(String[] args) {

	List&lt;String&gt; list = new ArrayList&lt;String&gt;();
	Collections.addAll(list, &quot;hi&quot;, null, &quot;hello&quot;);

	try {
		print(list);
	} catch (NullPointerException e) {
	System.out.println(e.getMessage());
	}
}

private static void print(List&lt;String&gt; list) {

	for (String string : list) {

		if (string == null) {
			throw new NullPointerException(&quot;String is null&quot;);
		}

		System.out.println(string);

	}
}

答案1

得分: 4

如果您想要打印整个列表,您应该使用System.out.println("String is null");而不是throw new NullPointerException("String is null");,因为异常会停止程序的运行,导致无法打印剩余的元素。

import java.util.*;
public class Program
{
    public static void main(String[] args) {

    List<String> list = new ArrayList<String>();
    Collections.addAll(list, "hi", null, "hello");

    try {
        print(list);
    } catch (NullPointerException e) {
        System.out.println(e.getMessage());
    }
}

private static void print(List<String> list) {

    for (String string : list) {
        if (string == null) {
            System.out.println("String is null");
        } else {
            System.out.println(string);
        }
    }
}
}
英文:

If you want to print the whole list, you should use System.out.println(&quot;String is null&quot;); instead of throw new NullPointerException(&quot;String is null&quot;); because the exception stops the program and it can't print the rest of the elements.

import java.util.*;
public class Program
{
	public static void main(String[] args) {

    List&lt;String&gt; list = new ArrayList&lt;String&gt;();
    Collections.addAll(list, &quot;hi&quot;, null, &quot;hello&quot;);

    try {
        print(list);
    } catch (NullPointerException e) {
    System.out.println(e.getMessage());
    }
}

private static void print(List&lt;String&gt; list) {

    for (String string : list) {
        if (string == null) {
            System.out.println(&quot;String is null&quot;);
        } else {
          System.out.println(string);
        }
    }
}
}

答案2

得分: 2

以下是翻译好的部分:

你可以通过检查可为空的变量来控制流程,而不是抛出空指针异常...

import java.util.*;
public class Program {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        Collections.addAll(list, "hi", null, "hello");
        print(list);
    }

    private static void print(List<String> list) {
        if(list == null ){
            return;
        }
        for (String string : list) {
            if (string == null) {
                System.out.println("String is null");
            } else {
                System.out.println(string);
            }
        }
    }
}
英文:

You can control your flow by checking nullable variables instead of throwing nullpointer exception...

import java.util.*;
public class Program {
    public static void main(String[] args) {
        List&lt;String&gt; list = new ArrayList&lt;String&gt;();
        Collections.addAll(list, &quot;hi&quot;, null, &quot;hello&quot;);
        print(list);
    }

    private static void print(List&lt;String&gt; list) {
        if(list == null ){
            return;
        }
        for (String string : list) {
            if (string == null) {
                System.out.println(&quot;String is null&quot;);
            } else {
                System.out.println(string);
            }
        }
    }
}

huangapple
  • 本文由 发表于 2020年9月27日 21:37:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64089045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定