-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#29 9 best practices to handle exceptions in java #76
#29 9 best practices to handle exceptions in java #76
Conversation
认领校对 |
|
||
[原文链接](https://dzone.com/articles/9-best-practices-to-handle-exceptions-in-java) | ||
|
||
> Whether you're brand new or an old pro, it's always good to brush up on exception handling practices to make sure you and your team can deal with problems. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
brush up on :复习。
当然了解也不影响阅读
translation/Java异常处理的9个最佳实践.md
Outdated
|
||
That’s why most development teams have their own set of rules on how to use them. And if you’re new to a team, you might be surprised how different these rules can be to the ones you’ve used before. | ||
|
||
这就是为何大多数开发团队都拥有一套自己的异常处理规范。如果你初进团队,你也许会发现这些规范和你曾使用的大相径庭。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你也许会发现这些规范和你曾使用的【】大相径庭。
以下9个最重要的实践方法能帮助你开始【】或改进你的异常处理
英文里面,这几处的宾语(或具体内容)都习惯性省略了,中文译文中最好补全
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其实文章翻译的还是很不错的,我提的一些意见仅代表我自己阅读时候的感受,请酌情处理。
但是有一点请注意,英文和中文之间的空格要加(输入法可以设置自动添加盘古之白)
translation/Java异常处理的9个最佳实践.md
Outdated
|
||
Another option is the try-with-resource statement which I explained in more detail in my [introduction to Java exception handling](https://stackify.com/specify-handle-exceptions-java/?utm_referrer=https%3A%2F%2Fdzone.com%2F#tryWithResource). | ||
|
||
你还可以选择try-with-resource语句,在我的这篇[“Java异常处理入门”](https://stackify.com/specify-handle-exceptions-java/?utm_referrer=https%3A%2F%2Fdzone.com%2F#tryWithResource)中有更为详细的介绍。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最好用书名号,或者不用任何标点,不要用引号
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hanxiaomax 请问标题也做同样的英文和中文之间加空格处理吗?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嗯
translation/Java异常处理的9个最佳实践.md
Outdated
|
||
The more specific the exception is that you throw, the better. Always keep in mind that a co-worker who doesn’t know your code, or maybe you in a few months, need to call your method and handle the exception. | ||
|
||
你抛出的异常约具体、明确越好。时刻牢记这点,特别是如果有一位并不了解你代码的同事,或几个月后的你需要调用自己的方法并处理异常时。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你抛出的异常约具体、明确越好
- 错别字
- 越具体,越明确
translation/Java异常处理的9个最佳实践.md
Outdated
``` | ||
|
||
## 3. Document the Exceptions You Specify | ||
## 3. 记录你所指定的异常 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
记录你所指定的异常
1.在文档中记录你使用的异常
2.为你的异常编写文档
两个建议。因为我感觉单纯说记录,容易和log搞混
translation/Java异常处理的9个最佳实践.md
Outdated
|
||
Don’t get me wrong; you shouldn’t write a paragraph of text. But you should explain the reason for the exception in 1-2 short sentences. That helps your operations team to understand the severity of the problem, and it also makes it easier for you to analyze any service incidents. | ||
|
||
别误会我的意思了。你没必要去写上一大段的文字,但你应该用一两句简短的话来解释一下异常发生的原因。这能让你的开发团队明白问题的严重性,也能让你更容易地分析服务意外事件。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
分析服务意外事件--》分析服务事故
translation/Java异常处理的9个最佳实践.md
Outdated
|
||
So, better don’t catch a Throwable unless you’re absolutely sure that you’re in an exceptional situation in which you’re able or required to handle an error. | ||
|
||
所以,最好不要在catch中使用Throwable,除非你确保自己处于一个能够处理错误,或被要求处理错误的特定情况下。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
除非你确保自己【处于】一个能够处理错误,或被要求处理错误的特定【情况下】。
这里谓语和宾语距离太远了,读起来就有点吃力,我觉得语序可以调整下。或者断句也行
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
所以,最好不要在catch中使用Throwable,除非你能确保自己处于一些特定情况下,比如你自己足以处理错误,又或被要求处理错误时。
translation/Java异常处理的9个最佳实践.md
Outdated
|
||
You should at least write a log message telling everyone that the unthinkable just had happened and that someone needs to check it. | ||
|
||
你应该至少把日志信息打印出来,告诉那些无意识下错误操作的人需要检查这里。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你应该至少把日志信息打印出来
--》
你至少应该
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里我觉得主要取决于至少修饰的范围
1.应该至少把日志打印出来:在所有应该打印的东西中(日志,debug等),要保证日志被打印出来了
2.至少应该把日志打印出来:在所有应该做的事情中(打印异常信息(包括日志debug)处理异常),至少要打印异常信息
我觉得2的范围实际上是比1大的,从原文来看,作者是说,不处理异常就不处理吧,既然你认为永远不可能出错,但是日志总要记录一下吧~
You should at least write a log message telling everyone that the unthinkable just had happened and that someone needs to check it.
我觉得很多时候会写成
You should ,at least, write a log message telling everyone that the unthinkable just had happened and that someone needs to check it.
作为插入语,那么我们就知道要去掉它不翻译。实际上就是:你应该记录日志!
(一个想法,不一定对
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我也觉得就是原意就是要把日志打印出来。
translation/Java异常处理的9个最佳实践.md
Outdated
|
||
The idea behind this best practice is similar to the two previous ones. But this time, you don’t provide the information to the caller of your method. The exception’s message gets read by everyone who has to understand what had happened when the exception was reported in the log file or your monitoring tool. | ||
|
||
这个方法背后的思想和前两个是类似的。但这一次,你不必给你的方法调用者提供信息。当异常在日志文件或监视工具中被报告时,任何一个想要了解发生了什么的人都要能读懂它。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exception’s message gets read by everyone who has to understand what had happened when the exception was reported in the log file or your monitoring tool.
首先我们划掉一部分,也是你understand理解错误的部分,这里干扰了你。
who has to understand what had happened:那些需要搞清楚问题出在哪的人
再看剩下的部分
The exception’s message gets read by everyone when the exception was reported in the log file or your monitoring tool.
我觉得应该就比较好理解了,简单来讲就是说,如果一个异常发生了,它的异常信息要么被打印到屏幕上(monitoring tool),要不就被记录在了log文件中,你总归是能够看到一个异常的信息的(不论是否详细)
再考虑一下上下文,作者说和前面两条类似,但是你不必在接口中添加说明了(我觉得主要强调的是不用写文档了),因为什么呢?因为异常信息和异常总是成对出现的,异常信息里面我们可以写的比较明晰一点
参考译文:
对于任何遭遇异常错误并需要搞清楚原因的人来说,异常信息总是在异常出现的同时,被记录在了日志中,或打印在了屏幕上。
@hanxiaomax 全部改过咯 😇 感谢校对 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
辛苦了
(提交翻译请使用下面的模板,其他类型pr请删除)
closes #29
1.译者信息
(转载时的署名信息,有需要请补充)
2.疑问区
4.将描述信息与异常一同抛出
第一段最后一句“当异常在日志文件或监视工具中被报告时,任何一个想要了解发生了什么的人都要能读懂它。”
我觉得这句自己翻的不是很符合中文习惯。
re:@Marticles 这句话是翻译错了,见这条意见
https://github.com/jobbole/translation-project/pull/76/files#r213213477
8.Don’t Log and Throw
这一段中的Log应该翻译成打印还是记录才更好呢?
re:@Marticles 我觉得打印就可以
3.自查表
4.发布信息(译者无需填写)
发布链接:http://www.importnew.com/29603.html
@hanxiaomax
请风哥校稿😇