Solved

Java, capturing a CTL-C in a hello world

Posted on 2013-01-04
6
373 Views
Last Modified: 2013-01-07
I want to 'catch' when the user of my simple app hit's control C

I want to write a program that prints 'hello world" in an infinite loop, but trys to catch wehn a ctl C , or any other key, then prints that he is done and exits.

How can I do this
0
Comment
Question by:Los Angeles1
  • 4
6 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 38745457
You'd have to wrap your code in a try - catch - finally block.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 38745489
Here is some Q&D code for it :

import java.util.Scanner;



class TryCatchFinally_ContendingWithCTRL_C extends Thread
{
      public static void main (String [] args)
      {

            new TryCatchFinally_ContendingWithCTRL_C().start();
            Scanner keyIn = new Scanner(System.in);
            
            int n1 = 0;
           	              
                try{
                System.out.print("Enter a number: \n");
                n1 = keyIn.nextInt( );
            	}catch(Exception e){}finally{System.out.println("Finally caught when you pressed CTRL-C");}

      }

	public void run(){

		try{}catch(Exception e){}finally{System.out.println("Finally routine from run() method.");}

	}
}

Open in new window


The class extends thread and outputs a string when run() comes to a natural end. But when you get the prompt on the screen to enter a number, instead of entering an int, hit CTRL-C, and you should get the result you want.

BUT beware - the finally part *ALWAYS* gets run, even if there was no exception.
0
 
LVL 16

Accepted Solution

by:
krakatoa earned 500 total points
ID: 38745516
And so if you want a version that only catches the error, you'd do smthg like :

import java.util.Scanner;



class TryCatchFinally_ContendingWithCTRL_C extends Thread
{

 static boolean error = false;

      public static void main (String [] args)
      {

            new TryCatchFinally_ContendingWithCTRL_C().start();
            Scanner keyIn = new Scanner(System.in);
            
            int n1 = 0;
           
           	              
                try{
                System.out.print("Enter a number: \n");
                n1 = keyIn.nextInt( );
            	}catch(Exception e){error=true;}finally{if(error){System.out.println("Finally caught when you pressed CTRL-C");}}

      }

	public void run(){

		try{}catch(Exception e){error = true;}finally{if(error){System.out.println("Finally routine from run() method.");}}

	}
}

Open in new window

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 86

Expert Comment

by:CEHJ
ID: 38746681
0
 

Author Comment

by:Los Angeles1
ID: 38751380
I tried the run() code, but when I hit a CTL-C, I still get the following

Terminate batch job (Y/N)? y

Open in new window

0
 
LVL 16

Expert Comment

by:krakatoa
ID: 38751623
No real idea what you mean by your last comment. Where did you ever mention a batch job being involved?
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now