Try/Catch exception help in Java
The program I am making has a console-based text menu to select an option between 1 and 2, I need it to catch for any input that isnt a number, and isnt a number between 1 and 2. This is what I have
Scanner scan = new Scanner(System.in);
int number = 0;
try {
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();
}
catch (ArithmeticException e) {
System.out.println("Arithmetic Exception");
}
catch (Exception e) {
if (!(number == 1) || !(number == 2)) {
System.out.println("Exception");
}
}
Any insight on where I have gone wrong is appreciated!
java
|
show 1 more comment
The program I am making has a console-based text menu to select an option between 1 and 2, I need it to catch for any input that isnt a number, and isnt a number between 1 and 2. This is what I have
Scanner scan = new Scanner(System.in);
int number = 0;
try {
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();
}
catch (ArithmeticException e) {
System.out.println("Arithmetic Exception");
}
catch (Exception e) {
if (!(number == 1) || !(number == 2)) {
System.out.println("Exception");
}
}
Any insight on where I have gone wrong is appreciated!
java
2
You say you’ve gone wrong - where does the actual behaviour differ from the intended?
– MTCoster
Nov 14 '18 at 20:00
1
What's the problem? You didn't specify.
– NiVeR
Nov 14 '18 at 20:01
2
Putting in the number3
, for example, isn't a system exception but a program-defined boundary. Use an if-statement for that inside the try-block.
– Drew Kennedy
Nov 14 '18 at 20:01
2
You don't get an ArithmeticException from reading an int from a Scanner, as you are not doing any arithmetic. And a number is always either not equal to 1 or not equal to 2.
– Andy Turner
Nov 14 '18 at 20:02
It won't throw an exception for you, you need to do the logic and throw the exception.
– SPlatten
Nov 14 '18 at 20:03
|
show 1 more comment
The program I am making has a console-based text menu to select an option between 1 and 2, I need it to catch for any input that isnt a number, and isnt a number between 1 and 2. This is what I have
Scanner scan = new Scanner(System.in);
int number = 0;
try {
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();
}
catch (ArithmeticException e) {
System.out.println("Arithmetic Exception");
}
catch (Exception e) {
if (!(number == 1) || !(number == 2)) {
System.out.println("Exception");
}
}
Any insight on where I have gone wrong is appreciated!
java
The program I am making has a console-based text menu to select an option between 1 and 2, I need it to catch for any input that isnt a number, and isnt a number between 1 and 2. This is what I have
Scanner scan = new Scanner(System.in);
int number = 0;
try {
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();
}
catch (ArithmeticException e) {
System.out.println("Arithmetic Exception");
}
catch (Exception e) {
if (!(number == 1) || !(number == 2)) {
System.out.println("Exception");
}
}
Any insight on where I have gone wrong is appreciated!
java
java
asked Nov 14 '18 at 19:59
Paul JonesPaul Jones
266
266
2
You say you’ve gone wrong - where does the actual behaviour differ from the intended?
– MTCoster
Nov 14 '18 at 20:00
1
What's the problem? You didn't specify.
– NiVeR
Nov 14 '18 at 20:01
2
Putting in the number3
, for example, isn't a system exception but a program-defined boundary. Use an if-statement for that inside the try-block.
– Drew Kennedy
Nov 14 '18 at 20:01
2
You don't get an ArithmeticException from reading an int from a Scanner, as you are not doing any arithmetic. And a number is always either not equal to 1 or not equal to 2.
– Andy Turner
Nov 14 '18 at 20:02
It won't throw an exception for you, you need to do the logic and throw the exception.
– SPlatten
Nov 14 '18 at 20:03
|
show 1 more comment
2
You say you’ve gone wrong - where does the actual behaviour differ from the intended?
– MTCoster
Nov 14 '18 at 20:00
1
What's the problem? You didn't specify.
– NiVeR
Nov 14 '18 at 20:01
2
Putting in the number3
, for example, isn't a system exception but a program-defined boundary. Use an if-statement for that inside the try-block.
– Drew Kennedy
Nov 14 '18 at 20:01
2
You don't get an ArithmeticException from reading an int from a Scanner, as you are not doing any arithmetic. And a number is always either not equal to 1 or not equal to 2.
– Andy Turner
Nov 14 '18 at 20:02
It won't throw an exception for you, you need to do the logic and throw the exception.
– SPlatten
Nov 14 '18 at 20:03
2
2
You say you’ve gone wrong - where does the actual behaviour differ from the intended?
– MTCoster
Nov 14 '18 at 20:00
You say you’ve gone wrong - where does the actual behaviour differ from the intended?
– MTCoster
Nov 14 '18 at 20:00
1
1
What's the problem? You didn't specify.
– NiVeR
Nov 14 '18 at 20:01
What's the problem? You didn't specify.
– NiVeR
Nov 14 '18 at 20:01
2
2
Putting in the number
3
, for example, isn't a system exception but a program-defined boundary. Use an if-statement for that inside the try-block.– Drew Kennedy
Nov 14 '18 at 20:01
Putting in the number
3
, for example, isn't a system exception but a program-defined boundary. Use an if-statement for that inside the try-block.– Drew Kennedy
Nov 14 '18 at 20:01
2
2
You don't get an ArithmeticException from reading an int from a Scanner, as you are not doing any arithmetic. And a number is always either not equal to 1 or not equal to 2.
– Andy Turner
Nov 14 '18 at 20:02
You don't get an ArithmeticException from reading an int from a Scanner, as you are not doing any arithmetic. And a number is always either not equal to 1 or not equal to 2.
– Andy Turner
Nov 14 '18 at 20:02
It won't throw an exception for you, you need to do the logic and throw the exception.
– SPlatten
Nov 14 '18 at 20:03
It won't throw an exception for you, you need to do the logic and throw the exception.
– SPlatten
Nov 14 '18 at 20:03
|
show 1 more comment
3 Answers
3
active
oldest
votes
If you want to throw an exception, this is easy but take care with the condition:
Scanner scan = new Scanner(System.in);
int number = 0;
try {
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();
if ((number != 1) && (number != 2)){
throw new Exception();
}
}
catch (InputMismatchException e) {
System.out.println("This is not a number");
}
catch (Exception e) {
System.out.println("Inside here because the number is not 1 or 2");
}
The condition
(number != 1) && (number != 2)
is true
if number
is not (1 or 2)
add a comment |
Scanner scan = new Scanner(System.in);
int number = 0;
try {
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();
if (number != 1 && number != 2){
throw new Exception();
}
}
catch (ArithmeticException e) {
System.out.println("Arithmetic Exception");
}
catch (Exception e) {
System.out.println("Inside here because the number is not 1 or 2 ");
}
add a comment |
As a suggestion, you can use
if ((number != 1) || (number != 2))
instead of
if (!(number == 1) || !(number == 2))
1
Sure, but why not just useif (true)
, that's equivalent and simpler again.
– Andy Turner
Nov 14 '18 at 20:15
add a comment |
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%2f53307911%2ftry-catch-exception-help-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to throw an exception, this is easy but take care with the condition:
Scanner scan = new Scanner(System.in);
int number = 0;
try {
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();
if ((number != 1) && (number != 2)){
throw new Exception();
}
}
catch (InputMismatchException e) {
System.out.println("This is not a number");
}
catch (Exception e) {
System.out.println("Inside here because the number is not 1 or 2");
}
The condition
(number != 1) && (number != 2)
is true
if number
is not (1 or 2)
add a comment |
If you want to throw an exception, this is easy but take care with the condition:
Scanner scan = new Scanner(System.in);
int number = 0;
try {
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();
if ((number != 1) && (number != 2)){
throw new Exception();
}
}
catch (InputMismatchException e) {
System.out.println("This is not a number");
}
catch (Exception e) {
System.out.println("Inside here because the number is not 1 or 2");
}
The condition
(number != 1) && (number != 2)
is true
if number
is not (1 or 2)
add a comment |
If you want to throw an exception, this is easy but take care with the condition:
Scanner scan = new Scanner(System.in);
int number = 0;
try {
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();
if ((number != 1) && (number != 2)){
throw new Exception();
}
}
catch (InputMismatchException e) {
System.out.println("This is not a number");
}
catch (Exception e) {
System.out.println("Inside here because the number is not 1 or 2");
}
The condition
(number != 1) && (number != 2)
is true
if number
is not (1 or 2)
If you want to throw an exception, this is easy but take care with the condition:
Scanner scan = new Scanner(System.in);
int number = 0;
try {
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();
if ((number != 1) && (number != 2)){
throw new Exception();
}
}
catch (InputMismatchException e) {
System.out.println("This is not a number");
}
catch (Exception e) {
System.out.println("Inside here because the number is not 1 or 2");
}
The condition
(number != 1) && (number != 2)
is true
if number
is not (1 or 2)
edited Nov 14 '18 at 20:26
answered Nov 14 '18 at 20:15
forpasforpas
13.6k3624
13.6k3624
add a comment |
add a comment |
Scanner scan = new Scanner(System.in);
int number = 0;
try {
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();
if (number != 1 && number != 2){
throw new Exception();
}
}
catch (ArithmeticException e) {
System.out.println("Arithmetic Exception");
}
catch (Exception e) {
System.out.println("Inside here because the number is not 1 or 2 ");
}
add a comment |
Scanner scan = new Scanner(System.in);
int number = 0;
try {
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();
if (number != 1 && number != 2){
throw new Exception();
}
}
catch (ArithmeticException e) {
System.out.println("Arithmetic Exception");
}
catch (Exception e) {
System.out.println("Inside here because the number is not 1 or 2 ");
}
add a comment |
Scanner scan = new Scanner(System.in);
int number = 0;
try {
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();
if (number != 1 && number != 2){
throw new Exception();
}
}
catch (ArithmeticException e) {
System.out.println("Arithmetic Exception");
}
catch (Exception e) {
System.out.println("Inside here because the number is not 1 or 2 ");
}
Scanner scan = new Scanner(System.in);
int number = 0;
try {
System.out.println("Enter a number ");
System.out.println("1.");
System.out.println("2.");
number = scan.nextInt();
if (number != 1 && number != 2){
throw new Exception();
}
}
catch (ArithmeticException e) {
System.out.println("Arithmetic Exception");
}
catch (Exception e) {
System.out.println("Inside here because the number is not 1 or 2 ");
}
edited Nov 14 '18 at 23:45
answered Nov 14 '18 at 20:11
Akeme UAkeme U
113
113
add a comment |
add a comment |
As a suggestion, you can use
if ((number != 1) || (number != 2))
instead of
if (!(number == 1) || !(number == 2))
1
Sure, but why not just useif (true)
, that's equivalent and simpler again.
– Andy Turner
Nov 14 '18 at 20:15
add a comment |
As a suggestion, you can use
if ((number != 1) || (number != 2))
instead of
if (!(number == 1) || !(number == 2))
1
Sure, but why not just useif (true)
, that's equivalent and simpler again.
– Andy Turner
Nov 14 '18 at 20:15
add a comment |
As a suggestion, you can use
if ((number != 1) || (number != 2))
instead of
if (!(number == 1) || !(number == 2))
As a suggestion, you can use
if ((number != 1) || (number != 2))
instead of
if (!(number == 1) || !(number == 2))
edited Nov 15 '18 at 5:53
Rahul Sharma
2,3191819
2,3191819
answered Nov 14 '18 at 20:09
kamilyrbkamilyrb
187
187
1
Sure, but why not just useif (true)
, that's equivalent and simpler again.
– Andy Turner
Nov 14 '18 at 20:15
add a comment |
1
Sure, but why not just useif (true)
, that's equivalent and simpler again.
– Andy Turner
Nov 14 '18 at 20:15
1
1
Sure, but why not just use
if (true)
, that's equivalent and simpler again.– Andy Turner
Nov 14 '18 at 20:15
Sure, but why not just use
if (true)
, that's equivalent and simpler again.– Andy Turner
Nov 14 '18 at 20:15
add a comment |
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%2f53307911%2ftry-catch-exception-help-in-java%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
2
You say you’ve gone wrong - where does the actual behaviour differ from the intended?
– MTCoster
Nov 14 '18 at 20:00
1
What's the problem? You didn't specify.
– NiVeR
Nov 14 '18 at 20:01
2
Putting in the number
3
, for example, isn't a system exception but a program-defined boundary. Use an if-statement for that inside the try-block.– Drew Kennedy
Nov 14 '18 at 20:01
2
You don't get an ArithmeticException from reading an int from a Scanner, as you are not doing any arithmetic. And a number is always either not equal to 1 or not equal to 2.
– Andy Turner
Nov 14 '18 at 20:02
It won't throw an exception for you, you need to do the logic and throw the exception.
– SPlatten
Nov 14 '18 at 20:03