我已经在StackOverFlow上阅读了有关已检查和未经检查的异常的多个帖子.老实说,我还是不太确定如何正确使用它们.
Joshua Bloch在" Effective Java "中说过
对可恢复条件使用已检查的异常,对编程错误使用运行时异常(第2版中的第58项)
让我们看看我是否正确理解这一点.
以下是我对已检查异常的理解:
try{
String userInput = //read in user input
Long id = Long.parseLong(userInput);
}catch(NumberFormatException e){
id = 0; //recover the situation by setting the id to 0
}
Run Code Online (Sandbox Code Playgroud)
1.以上是否考虑了检查异常?
2. RuntimeException是未经检查的异常吗?
以下是我对未经检查的异常的理解:
try{
File file = new File("my/file/path");
FileInputStream fis = new FileInputStream(file);
}catch(FileNotFoundException e){
//3. What should I do here?
//Should I "throw new FileNotFoundException("File not found");"?
//Should I log?
//Or should I System.exit(0);?
}
Run Code Online (Sandbox Code Playgroud)
4.现在,上述代码也不能成为检查异常吗?我可以尝试恢复这样的情况吗?我可以吗?(注意:我的第3个问题在catch上面)
try{
String …Run Code Online (Sandbox Code Playgroud) java exception runtimeexception checked-exceptions unchecked-exception
在Java中,我想做这样的事情:
try {
...
} catch (/* code to catch IllegalArgumentException, SecurityException,
IllegalAccessException, and NoSuchFieldException at the same time */) {
someCode();
}
Run Code Online (Sandbox Code Playgroud)
...代替:
try {
...
} catch (IllegalArgumentException e) {
someCode();
} catch (SecurityException e) {
someCode();
} catch (IllegalAccessException e) {
someCode();
} catch (NoSuchFieldException e) {
someCode();
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
try:
something here
except:
print('the whatever error occurred.')
Run Code Online (Sandbox Code Playgroud)
如何在except:块中打印错误/异常?
我有一个简单的属性的setter方法,null不适合这个特定的属性.我总是在这种情况下被撕裂:我应该扔一个IllegalArgumentException,还是一个NullPointerException?从javadocs看,两者似乎都合适.有某种理解标准吗?或者这只是你应该做的任何事情之一,而且两者都是正确的?
java null exception nullpointerexception illegalargumentexception
我最近开始用Ruby编程,我正在研究异常处理.
我想知道在C#中是否ensure相当于Ruby finally?我应该:
file = File.open("myFile.txt", "w")
begin
file << "#{content} \n"
rescue
#handle the error here
ensure
file.close unless file.nil?
end
Run Code Online (Sandbox Code Playgroud)
或者我应该这样做?
#store the file
file = File.open("myFile.txt", "w")
begin
file << "#{content} \n"
file.close
rescue
#handle the error here
ensure
file.close unless file.nil?
end
Run Code Online (Sandbox Code Playgroud)
是否ensure被调用无论即使一个异常没有什么引发,?
ruby error-handling exception-handling ruby-on-rails exception
NotImplementedExceptionJava中是否有类似.NET的东西?
我想知道在Python中指示无效参数组合的最佳实践.我遇到过一些你有这样功能的情况:
def import_to_orm(name, save=False, recurse=False):
"""
:param name: Name of some external entity to import.
:param save: Save the ORM object before returning.
:param recurse: Attempt to import associated objects as well. Because you
need the original object to have a key to relate to, save must be
`True` for recurse to be `True`.
:raise BadValueError: If `recurse and not save`.
:return: The ORM object.
"""
pass
Run Code Online (Sandbox Code Playgroud)
唯一令人烦恼的是,每个包装都有自己的,通常略有不同BadValueError.我知道在Java中存在java.lang.IllegalArgumentException- 是否很好理解每个人都将BadValueError在Python中创建自己的s或者是否有另一种首选方法?
我有一个方法,如果找到它,它应该返回一个对象.
如果找不到,我应该:
该noexcept关键字可以适当地应用于许多功能签名,但我不能确定何时我应该考虑在实践中使用它.根据我到目前为止所读到的内容,最后一分钟的添加noexcept似乎解决了移动构造函数抛出时出现的一些重要问题.但是,我仍然无法对一些实际问题提供令人满意的答案,这些问题使我首先要了解更多信息noexcept.
我知道永远不会抛出许多函数的例子,但编译器无法自行确定.noexcept在所有这些情况下我应该附加到函数声明吗?
不得不考虑我是否需要noexcept在每个函数声明之后追加,这将大大降低程序员的工作效率(坦率地说,这将是一个痛苦的屁股).对于哪些情况,我应该更加小心使用noexcept,以及在哪些情况下我可以使用暗示noexcept(false)?
我何时才能真实地期望在使用后观察到性能提升noexcept?特别是,给出一个代码示例,C++编译器在添加之后能够生成更好的机器代码noexcept.
就个人而言,我关心的是noexcept因为编译器提供了更大的自由来安全地应用某些类型的优化.现代编译器是否noexcept以这种方式利用?如果没有,我可以期待他们中的一些人在不久的将来这样做吗?