如果操作捕获多个异常,如何确定捕获的异常类型?
这个例子应该更有意义:
try {
int x = doSomething();
} catch (NotAnInt | ParseError e) {
if (/* thrown error is NotAnInt */) { // line 5
// printSomething
} else {
// print something else
}
}
Run Code Online (Sandbox Code Playgroud)
在第5行,我如何检查捕获的异常?
我试过if (e.equals(NotAnInt.class)) {..}
但没有运气.
注意:NotAnInt
并且ParseError
我的项目中的类是扩展的Exception
.
我正在尝试将scrollTop函数用于我的程序,但我发现我正在编写大量重复代码.
这是一个例子:
<div id="table_contents >
<ul>
<li id="one"> title One </li>
<li id="two"> title Two </li>
</ul>
</div>
<div id="content">
<p id="target_one"> I am content one </p>
<p id="target_two"> I am content two </p>
</div>
Run Code Online (Sandbox Code Playgroud)
如果我点击'标题一'我想滚动到'我满足一个'同样为标题二和内容二.
这很容易与jQuery/JS一起使用
$("#one").click(function() {
$('html, body').animate({
scrollTop: $("#target_one").offset().top -20
}, 800);
});
Run Code Online (Sandbox Code Playgroud)
但是比如说我的目录中有15个元素,我想让每个可点击的内容都滚动到它的内容.为此,我必须为每个元素重复上述代码15次.
还有比这更好的方法吗?
我创建了一个小型Python程序,它将计算各种"随机"数字的概率,看看它们是否遵循本福德定律.
我的算法可能不是最有效的,但它仍然可以完成这项工作.
我的问题是,为什么numGen1不遵循规则,其中numGen2和numGen3遵循规则?
这与计算机生成数字的方式有关吗?
样本输出(n = 5000):
NumGen1:
Occurance of starting with 1 is 0.112
Occurance of starting with 2 is 0.104
Occurance of starting with 3 is 0.117
Occurance of starting with 4 is 0.117
Run Code Online (Sandbox Code Playgroud)
NumGen2:
Occurance of starting with 1 is 0.327
Occurance of starting with 2 is 0.208
Occurance of starting with 3 is 0.141
Occurance of starting with 4 is 0.078
Run Code Online (Sandbox Code Playgroud)
NumGen3:
Occurance of starting with 1 is 0.301
Occurance of starting with 2 is 0.176 …
Run Code Online (Sandbox Code Playgroud)