Familiarising with Exception Handling [duplicate]










0
















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();











share|improve this question















marked as duplicate by daniu, Raedwald java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 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
















0
















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();











share|improve this question















marked as duplicate by daniu, Raedwald java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 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














0












0








0









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();











share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 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













  • 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








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













1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer





























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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.






    share|improve this answer



























      0














      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.






      share|improve this answer

























        0












        0








        0







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 8:18









        EranEran

        289k37475561




        289k37475561















            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            How do I collapse sections of code in Visual Studio Code for Windows?

            ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ