Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

capturing ctrl-C exception

843810Oct 18 2005 — edited Oct 18 2005
Hello -

I'm new to Java so maybe this is a basic question: how can I capture ctrl-C in my program?

I used the following code in main() but it doesn't seem to work:
try {
     parseCmdline(args);
     validateCmdline();
     do {
          line=getStatsLine(counter);
          outputLine(line, counter);
          sleepThd.sleep(interval * 1000);
          ...
     } while (alive);
     if (summarySet) outputSummary(titlesStr, counter);
} catch (Exception ex) {
     if (ex instanceof InterruptedException) {
          if (summarySet) outputSummary(titlesStr, counter);
          System.exit(0);
     } else if (ex instanceof RuntimeException) {
          throw (RuntimeException) ex;
     }
}
I need to be able to detect a ctrl-C to print out some information before exiting. Any help is appreciated.

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 15 2005
Added on Oct 18 2005
2 comments
497 views