为什么 catch 异常总是称为 e

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

Why is the catch Exception always called e

问题

例如:

尝试 {
    int n = scan.nextInt();
    System.out.println("\n你输入的数字是:" + n);
} catch (Exception e) {
    System.out.println("你输入了一个无效的值。");
}
似乎每个带有 `catch` 语句的程序中都将异常称为 `Exception`,这是一种不成文的规则吗
英文:

For example:

try {
		int n = scan.nextInt();
		System.out.println("\nYour number is: "+n);
	}catch(Exception e) {
		System.out.println("You entered a value thats not valid.");

It seems like every program with a catch statement in it has the Exception called e. Is this like an unwritten rule or something?

答案1

得分: 2

因为惯例。给事物命名很难。拥有共同的词汇有助于理解彼此的代码。

英文:

Because of conventions. Naming things is hard. Having a common vocabulary helps understanding each other's code.

答案2

得分: 2

在我看来,没有这样的规定,只是更短而已...这取决于你和你的编程习惯。

英文:

IMHO, no there is no such rule, it is just shorter like that... It is up to you and to your programming habit.

答案3

得分: 2

驼峰命名法通常在Java中使用的原因相同,你可以使用蛇形命名法,但不建议,因为出于某种原因会使你的代码难以阅读。

String helloWorld;
String hello_world; //看起来有点奇怪,不是吗?
英文:

The same reason why camelCase is generally used in java for naming, you can use snake_case but it's not recommended and makes your code harder to read for whatever reason.

String helloWorld;
String hello_world;//seems a bit off doesn't it.

huangapple
  • 本文由 发表于 2020年7月31日 18:15:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63189925.html
匿名

发表评论

匿名网友

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

确定