in a dotnet core application that uses pinvoke and runs on linux, when c++ throws - for example - std::runtime_error - we get:  terminate called recursively terminate called after throwing an instance of 'terminate called after throwing an instance of 'std::runtime_error*' Aborted (core dumped)   even though the invocation of the externed c++ method is wrapped in a try catch block in the managed code.  how can this be caught and treated in the dotnet core managed code?
77532
In C# code can you catch a native exception thrown from deep in some unmanaged library? If so do you need to do anything differently to catch it or does a standard try...catch get it?
32377
I've programmed C and C++ for a long time and so far I've never used exceptions and try / catch. What are the benefits of using that instead of just having functions return error codes?
99042
I am currently writing a little bootstrap code for a service that can be run in the console. It essentially boils down to calling the OnStart() method instead of using the ServiceBase to start and stop the service (because it doesn't run the application if it isn't installed as a service and makes debugging a nightmare).  Right now I am using Debugger.IsAttached to determine if I should use ServiceBase.Run or [service].OnStart, but I know that isn't the best idea because some times end users want to run the service in a console (to see the output etc. realtime).  Any ideas on how I could determine if the Windows service controller started 'me', or if the user started 'me' in the console? Apparantly  is not the answer. I thought about using commandline args, but that seems 'dirty'.  I could always see about a try-catch statement around ServiceBase.Run, but that seems dirty. Edit: Try catch doesn't work.  I have a solution: putting it up here for all the other interested stackers:      public void Run()     {         if (Debugger.IsAttached || Environment.GetCommandLineArgs().Contains&lt;string&gt;("-console"))         {             RunAllServices();         }         else         {             try             {                 string temp = Console.Title;                 ServiceBase.Run((ServiceBase[])ComponentsToRun);             }             catch             {                 RunAllServices();             }         }     } // void Run      private void RunAllServices()     {         foreach (ConsoleService component in ComponentsToRun)         {             component.Start();         }         WaitForCTRLC();         foreach (ConsoleService component in ComponentsToRun)         {             component.Stop();         }     }   EDIT: There was another question on StackOverflow where the guy had problems with the Environment.CurrentDirectory being "C:\Windows\System32" looks like that may be the answer. I will test today.
92456
Alright, I have been doing the following (variable names have been changed):   FileInputStream fis = null; try {     fis = new FileInputStream(file);      ... process ...  } catch (IOException e) {     ... handle error ... } finally {     if (fis != null)         fis.close(); }    Recently, I started using FindBugs, which suggests that I am not properly closing streams.  I decide to see if there's anything that can be done with a finally{} block, and then I see, oh yeah, close() can throw IOException.  What are people supposed to do here?  The Java libraries throw too many checked exceptions.
83726
I have a regular C# code. I have no exceptions. I want to programmatically log the current stack trace for debugging purpose. Example:  public void executeMethod()  {     logStackTrace();     method(); }
81176
The following is okay: try {     Console.WriteLine(&quot;Before&quot;);      yield return 1;      Console.WriteLine(&quot;After&quot;); } finally {     Console.WriteLine(&quot;Done&quot;); }  The finally block runs when the whole thing has finished executing (IEnumerator&lt;T&gt; supports IDisposable to provide a way to ensure this even when the enumeration is abandoned before it finishes). But this is not okay: try {     Console.WriteLine(&quot;Before&quot;);      yield return 1;  // error CS1626: Cannot yield a value in the body of a try block with a catch clause      Console.WriteLine(&quot;After&quot;); } catch (Exception e) {     Console.WriteLine(e.Message); }  Suppose (for the sake of argument) that an exception is thrown by one or other of the WriteLine calls inside the try block. What's the problem with continuing the execution in catch block? Of course, the yield return part is (currently) unable to throw anything, but why should that stop us from having an enclosing try/catch to deal with exceptions thrown before or after a yield return? Update: There's an  - seems that they already have enough problems implementing the try/finally behaviour correctly! EDIT: The MSDN page on this error is: .  It doesn't explain why, though.
13479
Note that this function does not have a "{" and "}" body.  Just a try/catch block:  void func( void ) try {     ... } catch(...) {     ... }   Is this intentionally part of C++, or is this a g++ extension?  Is there any purpose to this other than bypass 1 level of {}?  I'd never heard of this until I ran into
119736
I have a try...except block in my code and When an exception is throw. I really just want to continue with the code because in that case, everything is still able to run just fine. The problem is if you leave the except: block empty or with a #do nothing, it gives you a syntax error. I can't use continue because its not in a loop. Is there a keyword i can use that tells the code to just keep going?
37422
I'm getting this error every few days. I won't see the error for a few days then I'll get a flurry of 20 or so all with in a minute or so.  I've been very thorough going throw my code so that I'm using this basic setup for my DB access.  try {   myConnection.Open();   mySqlDataAdapter.Fill(myDataTable);   myConnection.Close(); } Catch (Exception err) {   if (myConnection.State != ConnectionState.Closed) myConnection.Close();   throw err; }   The way I understand it this should execute my queries and immediately release the connection back to the pool but if something goes wrong with the query then I catch the excpetion close my connection then throw the error up, which eventually gets trapped at the application level and logs and emails me the error.  Even using this throughout my code I'm still running across the issue. What can I do to diagnose the root cause of the issue?
56379
I'm writing a wrapper class for a command line executable.  This exe accepts input from stdin until I hit Ctrl+C in the command prompt shell, in which case it prints output to stdout based on the input.  I want to simulate that Ctrl+C press in C# code, sending the kill command to a .NET Process object.  I've tried calling Process.Kill(), but that doesn't seem to give me anything in the process's StandardOutput StreamReader.  Might there be anything I'm not doing right?  Here's the code I'm trying to use:  ProcessStartInfo info = new ProcessStartInfo(exe, args); info.RedirectStandardError = true; info.RedirectStandardInput = true; info.RedirectStandardOutput = true; info.UseShellExecute = false; Process p = Process.Start(info);  p.StandardInput.AutoFlush = true; p.StandardInput.WriteLine(scriptcode);  p.Kill();  string error = p.StandardError.ReadToEnd(); if (!String.IsNullOrEmpty(error))  {     throw new Exception(error); } string output = p.StandardOutput.ReadToEnd();   The output is always empty, even though I get data back from stdout when I run the exe manually.  Edit: This is C# 2.0 by the way.
37260
In C# and in Java (and possibly other languages as well), variables declared in a "try" block are not in scope in the corresponding "catch" or "finally" blocks.  For example, the following code does not compile:  try {   String s = "test";   // (more code...) } catch {   Console.Out.WriteLine(s);  //Java fans: think "System.out.println" here instead }   In this code, a compile-time error occurs on the reference to s in the catch block, because s is only in scope in the try block.  (In Java, the compile error is "s cannot be resolved"; in C#, it's "The name 's' does not exist in the current context".)  The general solution to this issue seems to be to instead declare variables just before the try block, instead of within the try block:  String s; try {   s = "test";   // (more code...) } catch {   Console.Out.WriteLine(s);  //Java fans: think "System.out.println" here instead }   However, at least to me, (1) this feels like a clunky solution, and (2) it results in the variables having a larger scope than the programmer intended (the entire remainder of the method, instead of only in the context of the try-catch-finally).  My question is, what were/are the rationale(s) behind this language design decision (in Java, in C#, and/or in any other applicable languages)?
19204
I have a process in Linux that's getting a segmentation fault.  How can I tell it to generate a core dump when it fails?
23537
How do I stop a Java process gracefully in Linux and Windows?  When does Runtime.getRuntime().addShutdownHook get called, and when does it not?  What about finalizers, do they help here?  Can I send some sort of signal to a Java process from a shell?  I am looking for preferably portable solutions.
92302
In short, how do you unit test an error condition such as EINTR on a system call.  One particular example I'm working on, which could be a case all by itself, is whether it's necessary to call fclose again when it returns EOF with (errno==EINTR). The behavior depends on the implementation of fclose:  // Given an open FILE *fp while (fclose(fp)==EOF &amp;&amp; errno==EINTR) {     errno = 0; }   This call can be unsafe if fp freed when EINTR occurs. How can I test the error handling for when (errno==EINTR)?
19218
How do I execute a command-line program from C# and get back the STD OUT results? Specifically, I want to execute DIFF on two files that are programmatically selected and write the results to a text box.
34221
I guess the question says it all, but, what happens if someone closes a c++ console app? As in, clicks the "x" in the top corner. Does it instantly close? Does it throw some sort of exception? Is it undefined behavior?
27164
I have seen people say that it is bad form to use catch with no arguments, especially if that catch doesn't do anything:  StreamReader reader=new  StreamReader("myfile.txt"); try {   int i = 5 / 0; } catch   // No args, so it will catch any exception {} reader.Close();   However, this is considered good form:  StreamReader reader=new  StreamReader("myfile.txt"); try {   int i = 5 / 0; } finally   // Will execute despite any exception {   reader.Close(); }   As far as I can tell, the only difference between putting cleanup code in a finally block and putting cleanup code after the try..catch blocks is if you have return statements in your try block (in that case, the cleanup code in finally will run, but code after the try..catch will not).  Otherwise, what's so special about finally?