Member Avatar for mehnihma

can someone help me how how can catch CTRL C and print it out then stop the program?
Thanks

/**
 * 218-102 Lab 7
 * TestCircle.java
 *
 * @author JKang & JLeone
 *
 */
 
   import java.util.*;
   import java.awt.event.*;
   import javax.swing.*; 
   import java.awt.*;
	
   public class TestCircleB {
   
   
      public static void main(String [] args) {
      // read a radius of the circle from command line
      
         try {
         
            Scanner in = new Scanner (System.in);
            System.out.print("Enter a circle radius : ");
            double radius = in.nextDouble();
         
         
         
         
         
         // Instantiate a Circle object
            Circle aCircle = new Circle(radius);
         
         // Print current status of the circle
            System.out.println(aCircle);
         
         }
         
         
            catch (ArrayIndexOutOfBoundsException e) {
            
               System.out.println("Array Index Out Of Bounds Exception Occured ...");
            
            }
         
         
            catch (ShapeException e) {
            
               System.out.println("Shape Exception Occured ...");
            
            }
         
         
            catch (NumberFormatException e) {
            
               System.out.println("Number Format Exception Occurred ...");
            
            }
         	
            catch (InputMismatchException e) {
            
               System.out.println("Input Mismatch Exception Occured ...");
            
            }
         
         
         /* 
            catch (InterruptedException e) 
            {
               e.printStackTrace();
               System.out.println("Input Mismatch Exception Occured ...");
            }
         
         
         */
         
            catch(InterruptedException e) {
            // Restore the interrupted status
               Thread.currentThread().interrupt();
            }
      
      
      }
   }

Recommended Answers

All 2 Replies

can someone help me how how can catch CTRL C and print it out then stop the program?
Thanks

/**
 * 218-102 Lab 7
 * TestCircle.java
 *
 * @author JKang & JLeone
 *
 */
 
   import java.util.*;
   import java.awt.event.*;
   import javax.swing.*; 
   import java.awt.*;
	
   public class TestCircleB {
   
   
      public static void main(String [] args) {
      // read a radius of the circle from command line
      
         try {
         
            Scanner in = new Scanner (System.in);
            System.out.print("Enter a circle radius : ");
            double radius = in.nextDouble();
         
         
         
         
         
         // Instantiate a Circle object
            Circle aCircle = new Circle(radius);
         
         // Print current status of the circle
            System.out.println(aCircle);
         
         }
         
         
            catch (ArrayIndexOutOfBoundsException e) {
            
               System.out.println("Array Index Out Of Bounds Exception Occured ...");
            
            }
         
         
            catch (ShapeException e) {
            
               System.out.println("Shape Exception Occured ...");
            
            }
         
         
            catch (NumberFormatException e) {
            
               System.out.println("Number Format Exception Occurred ...");
            
            }
         	
            catch (InputMismatchException e) {
            
               System.out.println("Input Mismatch Exception Occured ...");
            
            }
         
         
         /* 
            catch (InterruptedException e) 
            {
               e.printStackTrace();
               System.out.println("Input Mismatch Exception Occured ...");
            }
         
         
         */
         
            catch(InterruptedException e) {
            // Restore the interrupted status
               Thread.currentThread().interrupt();
            }
      
      
      }
   }

you may want to try:

Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() { /*
       my shutdown code here
    */ }
 });

should be able to intercept the CTRL C, but only as an intermediate step before the JVM completely shutdowns itself, so it may not be what you are looking for.

The Java virtual machine shuts down in response to two kinds of events:

  • The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or
  • The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.

You may want to use a SignalHandler (sun.misc.SignalHandler) to intercept the SIGINT signal triggered by a CTRL-C (on Unix as well as Windows).
See here:http://www.ibm.com/developerworks/ibm/library/i-signalhandling/?open&l=409,t=grj,p=jsh and this:http://waelchatila.com/2006/01/13/1137143896635.html

Member Avatar for mehnihma

I got it, when I tryed it on PC, i was working on mac

catch (NoSuchElementException e) {
            
               System.out.println("CTRL C Program stops");
            
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.