-2

I Don't know whether it is possible in java or not; I want to get an input from user (from System.in) and then throw a compile error with that input. I mean if the user inputted the text "HELLO" then the program should throw a compile error: Compile Error: HELLO. I want an error that actually make the program stop executing at that point with that message.

Is this possible? If yes, How?

Actually I want to make a compile error during run-time!

The code will be like this:

public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
String str = in.nextLine();

//compileError(str); this part must be completed


    }
}

Is this possible?

amir na
  • 217
  • 1
  • 13
  • you mean run time error (Exceptions)? ... as compiler generates error on syntax – Shahrzad Mar 24 '19 at 12:26
  • do you want to write your own compiler? – user3469811 Mar 24 '19 at 12:28
  • No. I think it is actually impossible but I was not sure. I want an exact compile error not runtime error or Exception. – amir na Mar 24 '19 at 12:28
  • Wanting something that is impossible to get will lead to frustration and nothing else. Perhaps your question is an [XY Problem](http://xyproblem.info) in disguise and you should tell us the motivation behind your question and not the impossible way that you're trying to achieve it. – Hovercraft Full Of Eels Mar 24 '19 at 12:30
  • @aquestion Think that you are given a program that you don't know what it should do and it pass some tests on an online judge. you know what the input could be but don't know in which order they come and you should get the test cases without actually accessing the online judge's test cases. I want to make my program give me some errors to find out what is in each test case. – amir na Mar 24 '19 at 12:32
  • @HovercraftFullOfEels see my Comment above. I want to get an online judge test cases which is not accessible. But I have a program that passes all test cases. I am even not sure what the question actually is. I just have a working program and want to extract the input from online judge. The point is that if I make compile errors, online judge will say what error I made but it don't say anything about run-time errors. – amir na Mar 24 '19 at 12:34
  • Just create an exception named CompileError and throw it :p – beatrice Mar 24 '19 at 12:39
  • You cannot cause an error in compilation time by a future input! – ronginat Mar 24 '19 at 12:51

4 Answers4

3

What you are referring to is Exceptions not a compilation error. Compilation error is when there is something wrong in the syntax of your code and the Java compiler fails to generate byte code for your program for the JVM to execute it. So that happens before you can run the program.

So you can use an instance of a RuntimeException or any sub class of it in Java to terminate your program when "HELLO" is used as the input, here in my sample code InputMismatchException is thrown if the input is "HELLO", this exception being a sub class of RuntimeException or unchecked exception doesn't require the programmer to add a throws clause or handle it explicitly:

public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      String str = in.nextLine();
      if("HELLO".equals(str)){
         throw new InputMismatchException("This input is not allowed");
     }
}
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
1

You cannot throw a compile error when your code is executing. I think you meant a Runtime error that cause your code to fail at runtime.

Try something like this:

throw new RuntimeException(str);

You can also catch this 'error' if you surround your code with try/catch clauses.

try {
   // get the input
   throw new RuntimeException(str);
} catch (RuntimeException ex) {
   // ex contains the error details
   // ex.getMessage() will give you the user's input
}

RuntimeException is the most general exception for errors at runtime, you can use other exceptions, list of exceptions.

ronginat
  • 1,910
  • 1
  • 12
  • 23
1

You can make your program stop executing at that point. For example:

public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        if (str.equals("HELLO")) {
            System.out.println("Compile Error: " + str);
            System.exit(1);
        }
        // Do something else.
    }
}

Another way to do it is to throw an exception ... except than you will get an exception stacktrace as well as a message. In the context you are doing this, it probably doesn't matter.

Note that there are some potential problems with this:

  1. If the judge intercepts all output, then it may not be possible to get hold of the messages that your application produces.

  2. The judge may feed your application different inputs each time it judges it.


However, this is not a real compile error. Indeed, it doesn't make sense for your program to generate a real compile error.

Compile errors occur when code is being compiled, and they are output by the compiler. But a Java application cannot read input (from the user or the "judge") at compile time. It can read input at runtime ... but then it is too late for a compilation error.

You seem to have your terminology confused. I suggest you read these articles to understand what the various terms mean:


Your commented thus:

Think that you are given a program that you don't know what it should do and it pass some tests on an online judge. You know what the input could be but don't know in which order they come and you should get the test cases without actually accessing the online judge's test cases. I want to make my program give me some errors to find out what is in each test case.

and

I just have a working program and want to extract the input from online judge.

That is not a compilation error you are talking about. That is a runtime error, because you want / need it to happen when your program runs. Please refer to the links above for explanations of these terms.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

If your intent is to print a message and exit out of program you can do something like following

...
System.err.println("Your input has resulted in an error, program will terminate");
/* You can change the above text with whatever you want */
System.exit(1);

Alternately, you can always throw an object of Exception (or an object of a class derived from it) and don't catch it anywhere in your program when it bubbles, program will terminate.

/* add throws Exception in your main method */
throw new Exception("Your input has resulted in an error, program will terminate")

However it's not clear why you would specially look for throwing Compile error. Compile time errors are thrown by compiler when compiling a program. During execution you neither expect them nor try to throw them.

Gro
  • 1,613
  • 1
  • 13
  • 19