exception-handling

Throw checked exceptions

浪子不回头ぞ 提交于 2020-08-24 03:59:08
问题 A few of my methods in Java throw exceptions such as NoSuchElementException, IllegalArgumentException, etc. But when using these methods, these exceptions appear to be unchecked. In other words, caller of my methods is not required to do a try/catch on my methods that throw those exceptions. I read around it seems that Exceptions by default are "checked" and only Errors are the ones "unchecked". But somehow, the exceptions I throw are also unchecked. It is weird. How can I ensure that when my

Throw checked exceptions

╄→尐↘猪︶ㄣ 提交于 2020-08-24 03:59:08
问题 A few of my methods in Java throw exceptions such as NoSuchElementException, IllegalArgumentException, etc. But when using these methods, these exceptions appear to be unchecked. In other words, caller of my methods is not required to do a try/catch on my methods that throw those exceptions. I read around it seems that Exceptions by default are "checked" and only Errors are the ones "unchecked". But somehow, the exceptions I throw are also unchecked. It is weird. How can I ensure that when my

Is it valid to have finally block without try and catch?

泄露秘密 提交于 2020-08-21 05:07:11
问题 I am trying to use the finally block without using the try/catch blocks but getting the error in Eclipse. Can I use the finally block without using the try/catch blocks? 回答1: finally should have atleast a try block, catch is optional. The point of finally blocks is to make sure stuff gets cleaned up whether an exception is thrown or not. As per the JLS A finally clause ensures that the finally block is executed after the try block and any catch block that might be executed, no matter how

Does C++ have a way to ignore an exception from a function?

有些话、适合烂在心里 提交于 2020-08-04 09:55:33
问题 I created a function that throws an exception, but under some circumstances I want it to simply ignore this exception. I wrote my code like this, but it's not quite elegant: try { myFunction(); } catch (...) {} Does C++ another way to write this? 回答1: No, there isn't. you can follow what the standard does in this case which is to overload the function twice, once with std::nothrow_t and once without. use the later to wrap the first std::error_code my_function(std::nothrow_t) noexcept; void my

should return outside finally and is the exception handled perfectly?

这一生的挚爱 提交于 2020-08-03 05:50:19
问题 Should I not put the return from this method below in finally ? Pylint gives error for this saying: 3: return statement in finally block may swallow exception (lost-exception) def sendMessage(self, subject, msgContent, files, mailto): """ Send the email message Args: subject(string): subject for the email msgContent(string): email message Content files(List): list of files to be attached mailto(string): email address to be sent to """ msg = self.prepareMail(subject, msgContent, files, mailto)