Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Catching Errors

I've done some code to catch an error it catches it but then goes straight to it again

And prints like this

Enter Mark
(minus sign inputted to check for typing mistake) -
Wrong type of input
Exception in thread InputMismatchException blah blah

Here is the code

public static void GetID() //Function called to collect the student ID and eight marks
{

for (int c = 0; c < 2;c++)
{

System.out.println("Please Enter the student ID");
studentID[c] = kybd.next();


System.out.println("Please enter the eight exams marks");
for (int a = 0; a < 8; ++a)
{
System.out.println("Enter mark" + " " +( a + 1) + " ");
Error();
marks[c][a] = kybd.nextInt();

if (marks[c][a] > 20)
{
System.out.println("Entry out of score limit");
a = a ;
c = c ;
System.out.println("Enter mark" + " " +( a + 1) + " ");
Error();
marks[c][a] = kybd.nextInt();

}
else
if(marks[c][a] < -1 | marks[c][a] == 0)
{
System.out.println("Entry out of score limit");
a = a ;
c = c ;
System.out.println("Enter mark" + " " +( a + 1) + " ");
Error();
marks[c][a] = kybd.nextInt();

}





}

}
Display();
}

public static void Error()
{
try
{
marks[c][a] = kybd.nextInt();

}
catch (InputMismatchException ime)
{
System.out.println("Wrong type of input");

}
}
[1846 byte] By [Rico3k] at [2007-11-11 7:26:15]
# 1 Re: Catching Errors
Error();
marks[c][a] = kybd.nextInt();

It throws it again because you catch it in Error(); then you do it again in the following line.

also this does nothing but fill space..
a = a ;
c = c ;
Joe Beam at 2007-11-11 22:38:19 >
# 2 Re: Catching Errors
also this does nothing but fill space..
a = a ;
c = c ;

Yes indeed, in the source code, but I sincerely doubt that the compiler will include
statements in the class file.

As for exceptions, they are just noise if you only catch them without altering the flow of command.
sjalle at 2007-11-11 22:39:19 >