ama
ama

Reputation: 369

Prevent “Send error report to Microsoft”

I'm working on java application which perform some Runtime sub-process on files, for some files I got error cause the Send error report to Microsoft window to appear ,I need to handle this error programmatically, without showing this window to user. Please can anyone help ?

Upvotes: 1

Views: 214

Answers (2)

Anya Shenanigans
Anya Shenanigans

Reputation: 94829

To Suppress windows error reporting the .exe that is being invoked should not terminate with an unhandled exception. This only works if you have access to the source of the application.

Based on the WER Reference - you should use the Win32 API call WerAddExcludedApplication to add the specific .exe files that you are intending to ignore to the per-user ignore list - you could create a simple stub-application that allows you to add applications by name to the ignore list. Then when you invoke the application it does not trigger the error.

Similarly you could create another application to remove them using the WerRemoveExcludedApplication.

Alternatives are to use JNI/JNA to make a class to encapsulate this functionality rather than using Runtime.exec

Here is a simple example using Java Native Access (JNA), which is a simpler version of JNI (no C++ needed for the most part). Download the jna.jar and make it part of your project.

import com.sun.jna.Native;
import com.sun.jna.WString;
import com.sun.jna.win32.StdCallLibrary;

public class JNATest {

    public interface CLibrary extends StdCallLibrary {
        CLibrary INSTANCE = (CLibrary) Native.loadLibrary("wer.dll", 
            CLibrary.class);
        int WerAddExcludedApplication(WString name, boolean global);
        int WerRemoveExcludedApplication(WString name, boolean global);
    }

    public static void main(String[] args) {
        CLibrary.INSTANCE.WerAddExcludedApplication(new WString("C:\\foo.exe"), false);
        CLibrary.INSTANCE.WerRemoveExcludedApplication(new WString("C:\\foo.exe"), false);
    }
}

Basically, replace the new WString(...) value with the name of the application that you are intending to ignore. It should be ignored for the purposes of windows error reporting at that point.

Bear in mind that the wer.dll is only on Windows Vista and newer, so if this is a problem, then you may need to edit the registry entries manually.

Upvotes: 1

zkristic
zkristic

Reputation: 639

You can always use try-catch-finally statement:

try
{
    some code here (the code that is causing the error);
}
catch (Exception x)
{
    handle exception here;
}

It works for me...

EDIT Here is the link that can help you a little bit more:

http://www.exampledepot.com/egs/Java%20Language/TryCatch.html

Upvotes: 0

Related Questions