Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (anonymous):

Exception handling question (code included) I need to enter an exception handling part to this code here: import java.util.Scanner; public class Summation{ public static void main(String[] args) throws Exception{ Scanner input=new Scanner(new java.io.File(args[0])); int total=0; while(input.hasNext()){ total += input.nextInt(); } input.close(); System.out.println("Total = "+ total); } }

OpenStudy (anonymous):

I've tried using this exception handling code to enter into my program stated above but it doesn't work. Scanner sc = new Scanner(System.in); int numberInput; try{ System.out.print("Enter an integer: "); numberInput = sc.nextInt(); // } catch (InputMismatchException e){ e.printStackTrace(); } catch(Exception ex){ ex.printStackTrace(); }

OpenStudy (woodrow73):

Looks like it works to me.. ``` Scanner sc = new Scanner(System.in); int numberInput; try{ System.out.print("Enter an integer: "); numberInput = sc.nextInt(); // } catch (InputMismatchException e){ System.out.println("e 1"); e.printStackTrace(); } catch(Exception e){ System.out.println("e 2"); e.printStackTrace(); } ``` perhaps you're confused what e.printStackTrace() does.. it prints out the error. If you remove e.printStackTrace(); it will give no error. Upon entering "go" into the keyboard with my code, "e 1" will print out followed by an error. You can combine exception handling like this with a loop to provide input validation.

OpenStudy (anonymous):

@woodrow73 yes i understand that it works, but what doesn't work is when i tried to incorporate the exception handling code given into the code i posted. i still don't understand that either.

OpenStudy (e.mccormick):

Hint: What is the scope of ` int total=0; ` in what you did for your code?

OpenStudy (anonymous):

Is it the number input of int = 0?

OpenStudy (e.mccormick):

No. The scope... let me see.

OpenStudy (e.mccormick):

The basics of scope. In this example code: ``` a{ int x; b{ int y; c{ int z; } } } ``` x can be used in a, b, or c because the scope of a includes everything inside it. b and c are inside a, so they are in this scope. y can only be used in b and c. z can only be used in c. So the following code would make for errors: ``` a{ int x=0; b{ int y=1; c{ int z=2; } } print x+y+z; } ``` because I am trying in a to use y and z in a, but they only exist inside b and c, not a.

OpenStudy (e.mccormick):

So in that example, the scope of z is the function c and only the function c. The scope of z is the funtion a, which includes b and c. So when I ask what the scope of ` int total=0; ` is, I am asking where can you use that variable.

OpenStudy (anonymous):

So in this case the total of int will determine based on the code explained above? Since there's int x, y, and z and that all adds up to the total, right?

OpenStudy (e.mccormick):

Ah, but I can't use it outside of c because I used x. It will cause an error.

OpenStudy (e.mccormick):

The way to fix the example is this: ``` a{ int x, y, z; x=0 b{ y=1; c{ z=2; } } print x+y+z; } ``` Because I am declaring x, y, and z in a, they can all be used anywhere! Do you now see the problem with your try block as posted?

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!