Forcing java to make a compile error with the user input as messageRuntime vs Compile timeWhat is the difference between run-time error and compiler error?How do you detect if there's not input from user (java)java compilation: classname Vs classname with file-extensionCompile error during compilation of my simple Java fileMain method error in javaRunning a java .class program on an HTML page (not a JApplet) like in Eclipse console?java compiling error in command promptI/P-a string S.O/P: For each digit start from 0-9,print count of their occurrence in S.Print10 lines,each line contain 2 space separated integersWould it make any difference giving arguments using scanner class instead of command line arguments?Unable to initialize an object through constructors in Java: Erroneous CompilationHow can I print a pyramid of *
What does War Machine's "Canopy! Canopy!" line mean in "Avengers: Endgame"?
I made a mistake ordering ground coffee - will Expresso ground coffee work for a French Press?
Access to all elements on the page
Credit card offering 0.5 miles for every cent rounded up. Too good to be true?
Is the capacitor drawn or wired wrongly?
Should we freeze the number of people coming in to the study for Kaplan-Meier test
How to detach yourself from a character you're going to kill?
How can I offer a test ride while selling a bike?
How to provide realism without making readers think grimdark
Benefits of employing devices that support vlan trunking
Please help me identify this plane
Is there a term for this?
Does any lore text explain why the planes of Acheron, Gehenna, and Carceri are the alignment they are?
Setting extra bits in a bool makes it true and false at the same time
Can an old DSLR be upgraded to match modern smartphone image quality
How can Iron Man's suit withstand this?
Asking bank to reduce APR instead of increasing credit limit
Working in the USA for living expenses only; allowed on VWP?
How can I determine the spell save DC of a monster/NPC?
Pros and cons of writing a book review?
What is the right way to float a home lab?
Why use water tanks from a retired Space Shuttle?
Was the 1959 Tibetan Uprising really an uprising?
Can a class take a different class's spell in their ritual book?
Forcing java to make a compile error with the user input as message
Runtime vs Compile timeWhat is the difference between run-time error and compiler error?How do you detect if there's not input from user (java)java compilation: classname Vs classname with file-extensionCompile error during compilation of my simple Java fileMain method error in javaRunning a java .class program on an HTML page (not a JApplet) like in Eclipse console?java compiling error in command promptI/P-a string S.O/P: For each digit start from 0-9,print count of their occurrence in S.Print10 lines,each line contain 2 space separated integersWould it make any difference giving arguments using scanner class instead of command line arguments?Unable to initialize an object through constructors in Java: Erroneous CompilationHow can I print a pyramid of *
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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?
java compiler-errors
|
show 3 more comments
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?
java compiler-errors
you mean run time error (Exceptions)? ... as compiler generates error on syntax
– a question
Mar 24 at 12:26
do you want to write your own compiler?
– user3469811
Mar 24 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 at 12:28
Wanting something that is impossible to get will lead to frustration and nothing else. Perhaps your question is an XY Problem 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 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 at 12:32
|
show 3 more comments
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?
java compiler-errors
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?
java compiler-errors
java compiler-errors
asked Mar 24 at 12:23
amir naamir na
1265
1265
you mean run time error (Exceptions)? ... as compiler generates error on syntax
– a question
Mar 24 at 12:26
do you want to write your own compiler?
– user3469811
Mar 24 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 at 12:28
Wanting something that is impossible to get will lead to frustration and nothing else. Perhaps your question is an XY Problem 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 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 at 12:32
|
show 3 more comments
you mean run time error (Exceptions)? ... as compiler generates error on syntax
– a question
Mar 24 at 12:26
do you want to write your own compiler?
– user3469811
Mar 24 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 at 12:28
Wanting something that is impossible to get will lead to frustration and nothing else. Perhaps your question is an XY Problem 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 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 at 12:32
you mean run time error (Exceptions)? ... as compiler generates error on syntax
– a question
Mar 24 at 12:26
you mean run time error (Exceptions)? ... as compiler generates error on syntax
– a question
Mar 24 at 12:26
do you want to write your own compiler?
– user3469811
Mar 24 at 12:28
do you want to write your own compiler?
– user3469811
Mar 24 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 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 at 12:28
Wanting something that is impossible to get will lead to frustration and nothing else. Perhaps your question is an XY Problem 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 at 12:30
Wanting something that is impossible to get will lead to frustration and nothing else. Perhaps your question is an XY Problem 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 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 at 12:32
@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 at 12:32
|
show 3 more comments
5 Answers
5
active
oldest
votes
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:
If the judge intercepts all output, then it may not be possible to get hold of the messages that your application produces.
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:
- Compilation Error or Compiler Error - https://en.wikipedia.org/wiki/Compilation_error
- Runtime Error - https://techterms.com/definition/runtime_error
- Exception - https://techterms.com/definition/exception
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.
add a comment |
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");
add a comment |
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.
add a comment |
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.
add a comment |
Yes, this is possible by throwing an the Exception class. Below is a simple addition to your code that would solve your problem.
public class Main
public static void main(String[] args)
try
Scanner in = new Scanner(System.in);
String str = in.nextLine();
throw new Exception(str);
catch (Exception e)
System.err.println(e.getMessage);
Exception
is not compile time error, it's run time.
– Guy
Mar 24 at 12:41
Exception is not an unchecked exception. It is a checked exception extending the Throwable class, as you can see on this link: docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/… Only classes extending RuntimeException and instances of RuntimeException are unchecked. So yes, it's a compile time exception.
– Martin Pekár
Mar 24 at 15:31
stackoverflow.com/questions/9471837/…
– Guy
Mar 24 at 16:07
stackoverflow.com/questions/846103/runtime-vs-compile-time
– Guy
Mar 24 at 16:07
Don't know what I am supposed to with that.
– Martin Pekár
Mar 24 at 16:17
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323763%2fforcing-java-to-make-a-compile-error-with-the-user-input-as-message%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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:
If the judge intercepts all output, then it may not be possible to get hold of the messages that your application produces.
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:
- Compilation Error or Compiler Error - https://en.wikipedia.org/wiki/Compilation_error
- Runtime Error - https://techterms.com/definition/runtime_error
- Exception - https://techterms.com/definition/exception
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.
add a comment |
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:
If the judge intercepts all output, then it may not be possible to get hold of the messages that your application produces.
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:
- Compilation Error or Compiler Error - https://en.wikipedia.org/wiki/Compilation_error
- Runtime Error - https://techterms.com/definition/runtime_error
- Exception - https://techterms.com/definition/exception
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.
add a comment |
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:
If the judge intercepts all output, then it may not be possible to get hold of the messages that your application produces.
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:
- Compilation Error or Compiler Error - https://en.wikipedia.org/wiki/Compilation_error
- Runtime Error - https://techterms.com/definition/runtime_error
- Exception - https://techterms.com/definition/exception
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.
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:
If the judge intercepts all output, then it may not be possible to get hold of the messages that your application produces.
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:
- Compilation Error or Compiler Error - https://en.wikipedia.org/wiki/Compilation_error
- Runtime Error - https://techterms.com/definition/runtime_error
- Exception - https://techterms.com/definition/exception
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.
edited Mar 25 at 1:02
answered Mar 24 at 12:32
Stephen CStephen C
533k72593956
533k72593956
add a comment |
add a comment |
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");
add a comment |
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");
add a comment |
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");
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");
answered Mar 24 at 12:28
Amardeep BhowmickAmardeep Bhowmick
6,54421231
6,54421231
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 24 at 12:33
GroGro
50549
50549
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Mar 24 at 12:35
answered Mar 24 at 12:28
ronginatronginat
690113
690113
add a comment |
add a comment |
Yes, this is possible by throwing an the Exception class. Below is a simple addition to your code that would solve your problem.
public class Main
public static void main(String[] args)
try
Scanner in = new Scanner(System.in);
String str = in.nextLine();
throw new Exception(str);
catch (Exception e)
System.err.println(e.getMessage);
Exception
is not compile time error, it's run time.
– Guy
Mar 24 at 12:41
Exception is not an unchecked exception. It is a checked exception extending the Throwable class, as you can see on this link: docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/… Only classes extending RuntimeException and instances of RuntimeException are unchecked. So yes, it's a compile time exception.
– Martin Pekár
Mar 24 at 15:31
stackoverflow.com/questions/9471837/…
– Guy
Mar 24 at 16:07
stackoverflow.com/questions/846103/runtime-vs-compile-time
– Guy
Mar 24 at 16:07
Don't know what I am supposed to with that.
– Martin Pekár
Mar 24 at 16:17
|
show 2 more comments
Yes, this is possible by throwing an the Exception class. Below is a simple addition to your code that would solve your problem.
public class Main
public static void main(String[] args)
try
Scanner in = new Scanner(System.in);
String str = in.nextLine();
throw new Exception(str);
catch (Exception e)
System.err.println(e.getMessage);
Exception
is not compile time error, it's run time.
– Guy
Mar 24 at 12:41
Exception is not an unchecked exception. It is a checked exception extending the Throwable class, as you can see on this link: docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/… Only classes extending RuntimeException and instances of RuntimeException are unchecked. So yes, it's a compile time exception.
– Martin Pekár
Mar 24 at 15:31
stackoverflow.com/questions/9471837/…
– Guy
Mar 24 at 16:07
stackoverflow.com/questions/846103/runtime-vs-compile-time
– Guy
Mar 24 at 16:07
Don't know what I am supposed to with that.
– Martin Pekár
Mar 24 at 16:17
|
show 2 more comments
Yes, this is possible by throwing an the Exception class. Below is a simple addition to your code that would solve your problem.
public class Main
public static void main(String[] args)
try
Scanner in = new Scanner(System.in);
String str = in.nextLine();
throw new Exception(str);
catch (Exception e)
System.err.println(e.getMessage);
Yes, this is possible by throwing an the Exception class. Below is a simple addition to your code that would solve your problem.
public class Main
public static void main(String[] args)
try
Scanner in = new Scanner(System.in);
String str = in.nextLine();
throw new Exception(str);
catch (Exception e)
System.err.println(e.getMessage);
answered Mar 24 at 12:31
Martin PekárMartin Pekár
274
274
Exception
is not compile time error, it's run time.
– Guy
Mar 24 at 12:41
Exception is not an unchecked exception. It is a checked exception extending the Throwable class, as you can see on this link: docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/… Only classes extending RuntimeException and instances of RuntimeException are unchecked. So yes, it's a compile time exception.
– Martin Pekár
Mar 24 at 15:31
stackoverflow.com/questions/9471837/…
– Guy
Mar 24 at 16:07
stackoverflow.com/questions/846103/runtime-vs-compile-time
– Guy
Mar 24 at 16:07
Don't know what I am supposed to with that.
– Martin Pekár
Mar 24 at 16:17
|
show 2 more comments
Exception
is not compile time error, it's run time.
– Guy
Mar 24 at 12:41
Exception is not an unchecked exception. It is a checked exception extending the Throwable class, as you can see on this link: docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/… Only classes extending RuntimeException and instances of RuntimeException are unchecked. So yes, it's a compile time exception.
– Martin Pekár
Mar 24 at 15:31
stackoverflow.com/questions/9471837/…
– Guy
Mar 24 at 16:07
stackoverflow.com/questions/846103/runtime-vs-compile-time
– Guy
Mar 24 at 16:07
Don't know what I am supposed to with that.
– Martin Pekár
Mar 24 at 16:17
Exception
is not compile time error, it's run time.– Guy
Mar 24 at 12:41
Exception
is not compile time error, it's run time.– Guy
Mar 24 at 12:41
Exception is not an unchecked exception. It is a checked exception extending the Throwable class, as you can see on this link: docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/… Only classes extending RuntimeException and instances of RuntimeException are unchecked. So yes, it's a compile time exception.
– Martin Pekár
Mar 24 at 15:31
Exception is not an unchecked exception. It is a checked exception extending the Throwable class, as you can see on this link: docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/… Only classes extending RuntimeException and instances of RuntimeException are unchecked. So yes, it's a compile time exception.
– Martin Pekár
Mar 24 at 15:31
stackoverflow.com/questions/9471837/…
– Guy
Mar 24 at 16:07
stackoverflow.com/questions/9471837/…
– Guy
Mar 24 at 16:07
stackoverflow.com/questions/846103/runtime-vs-compile-time
– Guy
Mar 24 at 16:07
stackoverflow.com/questions/846103/runtime-vs-compile-time
– Guy
Mar 24 at 16:07
Don't know what I am supposed to with that.
– Martin Pekár
Mar 24 at 16:17
Don't know what I am supposed to with that.
– Martin Pekár
Mar 24 at 16:17
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323763%2fforcing-java-to-make-a-compile-error-with-the-user-input-as-message%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
you mean run time error (Exceptions)? ... as compiler generates error on syntax
– a question
Mar 24 at 12:26
do you want to write your own compiler?
– user3469811
Mar 24 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 at 12:28
Wanting something that is impossible to get will lead to frustration and nothing else. Perhaps your question is an XY Problem 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 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 at 12:32