考虑到你有这样的代码:
doSomething() // this method may throw a checked a exception
//do some assignements calculations
doAnotherThing() //this method may also throw the same type of checked exception
//more calls to methods and calculations, all throwing the same kind of exceptions.
Run Code Online (Sandbox Code Playgroud)
现在我知道,实际上在构造异常时会出现性能损失,特别是展开堆栈.我还阅读了几篇文章,指出在输入try/catch块时会有轻微的性能损失,但这些文章似乎都没有结论.
我的问题是,是否建议将try catch中的行保持最小?,即只在try子句中包含可以实际抛出正在捕获的异常的行.try子句中的代码运行速度较慢或导致性能下降吗?
但考虑到这一点,更重要的是最佳实践/更易读的解决方案:
try {
doSomething() // this method may throw a checked a exception
//do some assignements calculations
doAnotherThing() //this method may also throw the same type of checked exception
//more calls to methods and calculations, all throwing …Run Code Online (Sandbox Code Playgroud) 这可能看起来像一个原始问题,或者可以通过我不知道的简单实用程序库方法来完成。
目的是检查嵌套在两个对象下的布尔字段的值。
private boolean sourceWebsite(Registration registration) {
Application application = registration.getApplication();
if (application == null) {
return true;
}
Metadata metadata = application.getMetadata();
if (metadata == null) {
return true;
}
Boolean source = metadata.getSource();
if (source == null) {
return true;
}
return !source;
}
Run Code Online (Sandbox Code Playgroud)
我知道这可以一次完成if()。if为了可读性,我在这里添加了多个。
有没有一种方法可以简化上面的if语句,并有一个简单的实用工具类返回Boolean source父对象是否为null的值?
我问这个关于Java的主题,但我想它适用于许多语言.
考虑,
if(myVariable==null){
doSomethingAboutIt();
}
else carryOn(myVariable);
Run Code Online (Sandbox Code Playgroud)
和
try{
carryOn(MyVariable);
}catch(NullPointerException e ){
doSOmethingAboutIt();}
Run Code Online (Sandbox Code Playgroud)
这两个代码块是否基本相同?有没有理由选择第二种方法?当然,更好的是myVariable永远不会为null,但似乎检查它的最佳方法是做一个简单的if语句.
我知道进入一个catch块在执行一个程序时有一些显着的成本,但是,我想知道是否进入try {}块也有任何影响因此我开始在google中寻找一个有很多意见的答案,但没有基准测试所有.我发现的一些答案是:
然而他们并没有用事实回答我的问题,所以我决定自己尝试一下.
这就是我做的.我有一个这种格式的csv文件:
host;ip;number;date;status;email;uid;name;lastname;promo_code;
状态之后的所有内容都是可选的,甚至没有相应的; ,所以在解析验证时必须要查看值是否存在,这就是我想到的try/catch问题.
我在公司里继承的当前代码是这样的:
StringTokenizer st=new StringTokenizer(line,";");
String host = st.nextToken();
String ip = st.nextToken();
String number = st.nextToken();
String date = st.nextToken();
String status = st.nextToken();
String email = "";
try{
email = st.nextToken();
}catch(NoSuchElementException e){
email = "";
}
Run Code Online (Sandbox Code Playgroud)
它重复了用uid,name,lastname和promo_code为电子邮件做的事情.
我改变了一切:
if(st.hasMoreTokens()){
email = st.nextToken();
}
Run Code Online (Sandbox Code Playgroud)
事实上它表现得更快.解析没有可选列的文件时.以下是平均时间:
--- Trying:122 milliseconds
--- Checking:33 milliseconds
Run Code Online (Sandbox Code Playgroud)
然而,这就是让我感到困惑的原因和我要问的原因:当在CSV的所有8000行中运行带有可选列值的示例时,if()版本仍然比try/catch版本表现更好,所以我的问题是
try块是否真的对我的代码没有任何性能影响?
此示例的平均时间为:
--- Trying:105 milliseconds
--- Checking:43 milliseconds
Run Code Online (Sandbox Code Playgroud)
有人能解释一下这里发生了什么吗?
非常感谢
这是一个简单的问题:
您如何看待每次使用try catch的代码?
void myfunction() {
try {
instruction1();
}
catch (ExceptionType1 e) {
// some code
}
try {
instruction2();
}
catch (ExceptionType2 e) {
// some code
}
try {
instruction3();
}
catch (ExceptionType3 e) {
// some code
}
try {
instruction4();
}
catch (ExceptionType4 e) {
// some code
}
// etc
}
Run Code Online (Sandbox Code Playgroud)
我知道这太可怕了,但我想知道这是否会降低性能.
我需要将工作日名称解析为DayOfWeek. 星期名称可以是短('Mon')或长('Monday')格式。
目前我想出了这样的解决方案:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE", locale);
DayOfWeek dayOfWeek;
try {
dayOfWeek = DayOfWeek.from(dtf.parse(value));
}
catch (DateTimeException e) {
dtf = DateTimeFormatter.ofPattern("EEEE", locale);
dayOfWeek = DayOfWeek.from(dtf.parse(value));
}
Run Code Online (Sandbox Code Playgroud)
有更短的解决方案吗?
java ×6
try-catch ×4
conditional ×1
date ×1
exception ×1
if-statement ×1
java-8 ×1
performance ×1
readability ×1