I'm writing a java program that adds two numbers with any length(the input is string). it works well but the judge gives me 44 because it has "Runtime Error" what should i do?
Asked
Active
Viewed 1,398 times
0
-
1catch the exceptions. – SomeJavaGuy Dec 04 '15 at 09:46
-
there is no exception,it works for all the numbers – Bob 22 Dec 04 '15 at 09:47
-
So what are those runtime errors ? – Arnaud Dec 04 '15 at 09:48
-
i don't know .the console works perfectly .it gives no error in any kind. judge is the problem – Bob 22 Dec 04 '15 at 09:52
-
Show the code you have, can't solve the problem if we can't see it. – yitzih Dec 04 '15 at 09:53
-
Without any code or error log provided, it's virtually impossible for anyone to answer this. If the code is long as you say, paste it on a code-hosting site and link it here. – Calvin P. Dec 04 '15 at 09:55
-
Possible duplicate of [Handling RuntimeExceptions in Java](http://stackoverflow.com/questions/2028719/handling-runtimeexceptions-in-java) – Maheshwar Ligade Dec 04 '15 at 10:07
2 Answers
2
To Answer your question "How to handle runtime-errors",
It is not different from any other exception:
try {
someCode();
} catch (RuntimeException ex) {
//handle runtime exception here
}
This judge may have given you a 44 (assuming that is low) because the input that comes to you as strings may not be numbers at all, and if this happens, your program should not crash? That would be my guess
UPDATE: Now that you have some code up, this is most likely the case, what happens if String a is "hello" ? Your program would crash at Long.parseLong()
, you need to handle this!

Manish Mallavarapu
- 425
- 3
- 12
0
Replace all your calls to Long.parseLong
, by calls to such a method :
private long checkLong(String entry){
long result = 0;
try
{
result = Long.parseLong(entry);
}
catch(NumberFormatException e)
{
System.out.println("Value " + entry + " is not valid") ;
System.exit(1);
}
return result;
}

Arnaud
- 17,229
- 3
- 31
- 44