try-catch

try-catch exceptions in Swift [duplicate]

房东的猫 提交于 2019-12-17 04:30:45
问题 This question already has answers here : Error-Handling in Swift-Language (12 answers) Closed 5 years ago . Is it possible to catch exceptions in Swift? Given the following code: NSException.raise(NSRangeException, format: "Now you've gone too far!", arguments: CVaListPointer(fromUnsafePointer: UnsafePointer())) Is it possible to prevent the exception from crashing the entire program? That is, what is the Swift equivalent of the following in Objective-C: @try { [NSException raise

How do exceptions work (behind the scenes) in c++

对着背影说爱祢 提交于 2019-12-17 02:52:22
问题 I keep seeing people say that exceptions are slow, but I never see any proof. So, instead of asking if they are, I will ask how do exceptions work behind the scene, so I can make a decisions of when to use them and if they are slow. From what I know, exceptions are the same thing as doing a bunch of return but it also checks when it needs to stop doing the return. How does it check when to do stop? I am taking a guess and saying there is a second stack which holds the type of exception and

Program freezes during Thread.sleep() and with Timer

混江龙づ霸主 提交于 2019-12-17 02:26:29
问题 Original question: This method is supposed to change the image being displayed on a JFrame gradually into another image. However, without some way to slow it down, it just seems to change from one image to the new image. In order to slow it down, I put in a Thread.sleep(1000) so the changes wouldn't happen instantly. However, with this line in there, my program freezes completely. No error message, no nothing. Can anyone please help me out? Suggest a better method to slow it down, or how this

catch exception by pointer in C++

我与影子孤独终老i 提交于 2019-12-17 02:11:49
问题 I found that there are three ways to catch an exception, what are the differences? 1) catch by value; 2) catch by reference; 3) catch by pointer; I only know that catch by value will invoke two copies of the object, catch by reference will invoke one. So how about catch by pointer? When to use catch by pointer? In addition to throw an object, can I throw a pointer to an object like this? class A {} void f() { A *p = new A(); throw p; } 回答1: The recommended way is to throw by value and catch

Why catch and rethrow an exception in C#?

安稳与你 提交于 2019-12-16 20:08:45
问题 I'm looking at the article C# - Data Transfer Object on serializable DTOs. The article includes this piece of code: public static string SerializeDTO(DTO dto) { try { XmlSerializer xmlSer = new XmlSerializer(dto.GetType()); StringWriter sWriter = new StringWriter(); xmlSer.Serialize(sWriter, dto); return sWriter.ToString(); } catch(Exception ex) { throw ex; } } The rest of the article looks sane and reasonable (to a noob), but that try-catch-throw throws a WtfException... Isn't this exactly

Swift 2.1 do-try-catch not catching error

杀马特。学长 韩版系。学妹 提交于 2019-12-14 04:19:37
问题 Here's my Swift 2.1 code snippet. The error that's occurring is shown in the comments at the point where the error appears. The error shows up in the debugging panel, and the app crashes. The app never prints the line in the catch, nor does it gracefully return as expected. let audioFileURL = receivedAudio.filePathURL guard let audioFile = try? AVAudioFile(forReading: audioFileURL) else { print("file setup failed") return } let audioFileFrameCount = AVAudioFrameCount(audioFile.length)

When would you prefer to declare an exception rather than handling it in Java?

删除回忆录丶 提交于 2019-12-14 04:19:12
问题 I know we can declare the exception for our method if we want it to be handled by the calling method. This will even allow us to do stuff like write to the OutputStream without wrapping the code in try/catch block if the enclosing method throws IOException. My question is: Can anyone provide an instance where this is usually done where you'd like the calling method to handle the exception instead of the current method? Edit: I meant calling method instead of super class in the last line. 回答1:

Why does the Try-Catch block affect a variable in an enclosing scope?

时光总嘲笑我的痴心妄想 提交于 2019-12-14 03:45:06
问题 Why does the outer temp become empty after catching first exception? #include <iostream> int main() { std::string temp("exception"); int value; while(std::cin>> value && value != 0) { try{ if(value > 9) throw temp; else std::cout << value << "\n"; } catch(std::string temp) { std::cout << temp << "\n"; } } return 0; } Input: 1 2 11 13 Output: 1 2 exception // Printing Empty string Expected Output: 1 2 exception exception I compile my code with g++ 7.3.0. 回答1: This appears to be a bug in GCC's

Static type of the exception object

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-14 01:38:37
问题 I read the following from C++ Primer (5th edition, Section 18.1.1): "When we throw an expression, the static, compile-time type of that expression determines the type of the exception object." So I tried the following code: #include <iostream> class Base{ public: virtual void print(std::ostream& os){os << "Base\n";} }; class Derived: public Base{ public: void print(std::ostream& os){os << "Derived\n";} }; int main(){ try{ Derived d; Base &b = d; b.print(std::cout); //line 1 throw b; } catch

C# How to catch exception in a Task without block UI using wait

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 18:39:41
问题 I have this code in a button click private async void btnStart_Click(object sender, EventArgs e) { Msg.Clear(); stopWatch.Reset(); timer.Start(); stopWatch.Start(); lblTime.Text = stopWatch.Elapsed.TotalSeconds.ToString("#"); progressBar.MarqueeAnimationSpeed = 30; try { await Task.Factory.StartNew(() => { try { Reprocess(); } catch (Exception ex) { Msg.Add(new clsMSG(ex.Message, "Error", DateTime.Now)); timer.Stop(); stopWatch.Stop(); throw; } }); } catch { throw; } } and this on the