Familiarising with Exception Handling [duplicate]
This question already has an answer here:
What is a debugger and how can it help me diagnose problems?
2 answers
I can't figure out why my getHours() is catching InputMismatchException when I have only thrown InvalidHrExcep to the method itself. Can anyone enlighten me?
public static void main (String args)
do
try
getHours();
getAmPm();
accept = true;
catch(InvalidHrExcep h)
System.out.println (h);
accept = false;
console.nextLine();
catch (InputMismatchException e)
System.out.println (e);
System.out.println("Input must be AM or PM.");
accept = false;
console.nextLine();
while(accept == false);
My method for getHours()
public static int getHours() throws InvalidHrExcep
System.out.print ("Enter hours: ");
int hour = console.nextInt();
if(hour > 0 && hour <= 12)
return hour;
else
throw new InvalidHrExcep();
java exception exception-handling
marked as duplicate by daniu, Raedwald
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 9:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 2 more comments
This question already has an answer here:
What is a debugger and how can it help me diagnose problems?
2 answers
I can't figure out why my getHours() is catching InputMismatchException when I have only thrown InvalidHrExcep to the method itself. Can anyone enlighten me?
public static void main (String args)
do
try
getHours();
getAmPm();
accept = true;
catch(InvalidHrExcep h)
System.out.println (h);
accept = false;
console.nextLine();
catch (InputMismatchException e)
System.out.println (e);
System.out.println("Input must be AM or PM.");
accept = false;
console.nextLine();
while(accept == false);
My method for getHours()
public static int getHours() throws InvalidHrExcep
System.out.print ("Enter hours: ");
int hour = console.nextInt();
if(hour > 0 && hour <= 12)
return hour;
else
throw new InvalidHrExcep();
java exception exception-handling
marked as duplicate by daniu, Raedwald
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 9:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
your getHours isn't catching anything. it might throw it, which is a different matter. I would recommend adding e.printStackTrace(); into your catch block, going through the stacktrace 'll make it easier to see where the exception is thrown. so far, there is no reason to believe it was thrown by getHours()
– Stultuske
Nov 13 '18 at 8:10
2
We have no idea whatgetHours()does.
– Eran
Nov 13 '18 at 8:11
Oops, ok I have added my getHours() method.
– Zac Ee
Nov 13 '18 at 8:14
can you show the implementation of InvalidHrExcep if it is a Exception you have implemented?
– Sand
Nov 13 '18 at 8:16
what did you provide as input for the getHours() method? nextInt might throw that exception, if the input you've entered is not a valid int
– Stultuske
Nov 13 '18 at 8:16
|
show 2 more comments
This question already has an answer here:
What is a debugger and how can it help me diagnose problems?
2 answers
I can't figure out why my getHours() is catching InputMismatchException when I have only thrown InvalidHrExcep to the method itself. Can anyone enlighten me?
public static void main (String args)
do
try
getHours();
getAmPm();
accept = true;
catch(InvalidHrExcep h)
System.out.println (h);
accept = false;
console.nextLine();
catch (InputMismatchException e)
System.out.println (e);
System.out.println("Input must be AM or PM.");
accept = false;
console.nextLine();
while(accept == false);
My method for getHours()
public static int getHours() throws InvalidHrExcep
System.out.print ("Enter hours: ");
int hour = console.nextInt();
if(hour > 0 && hour <= 12)
return hour;
else
throw new InvalidHrExcep();
java exception exception-handling
This question already has an answer here:
What is a debugger and how can it help me diagnose problems?
2 answers
I can't figure out why my getHours() is catching InputMismatchException when I have only thrown InvalidHrExcep to the method itself. Can anyone enlighten me?
public static void main (String args)
do
try
getHours();
getAmPm();
accept = true;
catch(InvalidHrExcep h)
System.out.println (h);
accept = false;
console.nextLine();
catch (InputMismatchException e)
System.out.println (e);
System.out.println("Input must be AM or PM.");
accept = false;
console.nextLine();
while(accept == false);
My method for getHours()
public static int getHours() throws InvalidHrExcep
System.out.print ("Enter hours: ");
int hour = console.nextInt();
if(hour > 0 && hour <= 12)
return hour;
else
throw new InvalidHrExcep();
This question already has an answer here:
What is a debugger and how can it help me diagnose problems?
2 answers
java exception exception-handling
java exception exception-handling
edited Nov 13 '18 at 8:19
Zac Ee
asked Nov 13 '18 at 8:09
Zac EeZac Ee
42
42
marked as duplicate by daniu, Raedwald
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 9:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by daniu, Raedwald
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 9:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
your getHours isn't catching anything. it might throw it, which is a different matter. I would recommend adding e.printStackTrace(); into your catch block, going through the stacktrace 'll make it easier to see where the exception is thrown. so far, there is no reason to believe it was thrown by getHours()
– Stultuske
Nov 13 '18 at 8:10
2
We have no idea whatgetHours()does.
– Eran
Nov 13 '18 at 8:11
Oops, ok I have added my getHours() method.
– Zac Ee
Nov 13 '18 at 8:14
can you show the implementation of InvalidHrExcep if it is a Exception you have implemented?
– Sand
Nov 13 '18 at 8:16
what did you provide as input for the getHours() method? nextInt might throw that exception, if the input you've entered is not a valid int
– Stultuske
Nov 13 '18 at 8:16
|
show 2 more comments
1
your getHours isn't catching anything. it might throw it, which is a different matter. I would recommend adding e.printStackTrace(); into your catch block, going through the stacktrace 'll make it easier to see where the exception is thrown. so far, there is no reason to believe it was thrown by getHours()
– Stultuske
Nov 13 '18 at 8:10
2
We have no idea whatgetHours()does.
– Eran
Nov 13 '18 at 8:11
Oops, ok I have added my getHours() method.
– Zac Ee
Nov 13 '18 at 8:14
can you show the implementation of InvalidHrExcep if it is a Exception you have implemented?
– Sand
Nov 13 '18 at 8:16
what did you provide as input for the getHours() method? nextInt might throw that exception, if the input you've entered is not a valid int
– Stultuske
Nov 13 '18 at 8:16
1
1
your getHours isn't catching anything. it might throw it, which is a different matter. I would recommend adding e.printStackTrace(); into your catch block, going through the stacktrace 'll make it easier to see where the exception is thrown. so far, there is no reason to believe it was thrown by getHours()
– Stultuske
Nov 13 '18 at 8:10
your getHours isn't catching anything. it might throw it, which is a different matter. I would recommend adding e.printStackTrace(); into your catch block, going through the stacktrace 'll make it easier to see where the exception is thrown. so far, there is no reason to believe it was thrown by getHours()
– Stultuske
Nov 13 '18 at 8:10
2
2
We have no idea what
getHours() does.– Eran
Nov 13 '18 at 8:11
We have no idea what
getHours() does.– Eran
Nov 13 '18 at 8:11
Oops, ok I have added my getHours() method.
– Zac Ee
Nov 13 '18 at 8:14
Oops, ok I have added my getHours() method.
– Zac Ee
Nov 13 '18 at 8:14
can you show the implementation of InvalidHrExcep if it is a Exception you have implemented?
– Sand
Nov 13 '18 at 8:16
can you show the implementation of InvalidHrExcep if it is a Exception you have implemented?
– Sand
Nov 13 '18 at 8:16
what did you provide as input for the getHours() method? nextInt might throw that exception, if the input you've entered is not a valid int
– Stultuske
Nov 13 '18 at 8:16
what did you provide as input for the getHours() method? nextInt might throw that exception, if the input you've entered is not a valid int
– Stultuske
Nov 13 '18 at 8:16
|
show 2 more comments
1 Answer
1
active
oldest
votes
console.nextInt() can definitely throw InputMismatchException exception, and your getHours method doesn't have to specify that exception in a throws clause, since it's an unchecked exception.
If you don't want getHours() to throw that exception, you should put console.nextInt() in a try block and catch it.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
console.nextInt() can definitely throw InputMismatchException exception, and your getHours method doesn't have to specify that exception in a throws clause, since it's an unchecked exception.
If you don't want getHours() to throw that exception, you should put console.nextInt() in a try block and catch it.
add a comment |
console.nextInt() can definitely throw InputMismatchException exception, and your getHours method doesn't have to specify that exception in a throws clause, since it's an unchecked exception.
If you don't want getHours() to throw that exception, you should put console.nextInt() in a try block and catch it.
add a comment |
console.nextInt() can definitely throw InputMismatchException exception, and your getHours method doesn't have to specify that exception in a throws clause, since it's an unchecked exception.
If you don't want getHours() to throw that exception, you should put console.nextInt() in a try block and catch it.
console.nextInt() can definitely throw InputMismatchException exception, and your getHours method doesn't have to specify that exception in a throws clause, since it's an unchecked exception.
If you don't want getHours() to throw that exception, you should put console.nextInt() in a try block and catch it.
answered Nov 13 '18 at 8:18
EranEran
289k37475561
289k37475561
add a comment |
add a comment |
1
your getHours isn't catching anything. it might throw it, which is a different matter. I would recommend adding e.printStackTrace(); into your catch block, going through the stacktrace 'll make it easier to see where the exception is thrown. so far, there is no reason to believe it was thrown by getHours()
– Stultuske
Nov 13 '18 at 8:10
2
We have no idea what
getHours()does.– Eran
Nov 13 '18 at 8:11
Oops, ok I have added my getHours() method.
– Zac Ee
Nov 13 '18 at 8:14
can you show the implementation of InvalidHrExcep if it is a Exception you have implemented?
– Sand
Nov 13 '18 at 8:16
what did you provide as input for the getHours() method? nextInt might throw that exception, if the input you've entered is not a valid int
– Stultuske
Nov 13 '18 at 8:16