Can anyone explain how to handle the Runtime Exceptions in Java?
-
I think the non-accepters of SO have figured out by now that those are empty threats. We want mod points! – danben Jan 08 '10 at 15:53
-
1i never seen accept button anywhere ? – RKCY Jan 08 '10 at 16:00
-
It's the big tick / correction mark beside an answer. – Gordon Jan 08 '10 at 16:02
-
3It's sad, but I have the impression that obvious 30-second answers to dumb questions usually give more reward points than well-written answers to more interesting questions. For questions with a high view rate, the first answer usually ends up with a lot of reward points even if it is not related to the question at all. – jarnbjo Jan 08 '10 at 16:03
-
sorry, i could not find anywhere. – RKCY Jan 08 '10 at 16:05
-
jarnbjo, that is a sad truth. eventually you'll get used to now being properly rewarded for your good and professional answers on hard questions ;) – Bozho Jan 08 '10 at 16:05
-
@kalyaniRavi: it's the check mark underneath the score for an answer. – danben Jan 08 '10 at 16:16
-
@Danben: i checked, But i could not find the check mark underneath the score for accept the answer. – RKCY Jan 08 '10 at 19:10
-
@kalyaniRavi: see, I took a screenshot, there is a clickable mark below the score and vote buttons: http://gregory.pakosz.fr/stackoverflow/checkmark.png – Gregory Pakosz Jan 09 '10 at 18:20
4 Answers
It doesn't differ from handling a regular exception:
try {
someMethodThatThrowsRuntimeException();
} catch (RuntimeException ex) {
// do something with the runtime exception
}

- 588,226
- 146
- 1,060
- 1,140
-
1
-
5
-
I dont see a problem with this question - I had a Runnable that I suspected was causing a runtime error and I wasn't aware that you could just wrap any code in a try/catch & add a RunTimeException catch. I Googled'Android handling runtime exceptions', this was the first question that came up & this answer provided what I needed. Surely thats exactly how SO should work? – Mitch Jun 12 '15 at 19:18
If you know the type of Exception that might be thrown, you could catch it explicitly. You could also catch Exception
, but this is generally considered to be very bad practice because you would then be treating Exceptions of all types the same way.
Generally the point of a RuntimeException is that you can't handle it gracefully, and they are not expected to be thrown during normal execution of your program.

- 80,905
- 18
- 123
- 145
-
Except, perhaps, when a custom component is throwing a RuntimeException because it can not reach a certain host over network and that is crashing your Android app. – Alen Siljak Aug 24 '16 at 09:28
You just catch them, like any other exception.
try {
somethingThrowingARuntimeException()
}
catch (RuntimeException re) {
// Do something with it. At least log it.
}

- 16,256
- 8
- 46
- 71
Not sure if you're referring directly to RuntimeException
in Java, so I'll assume you're talking about run-time exceptions.
The basic idea of exception handling in Java is that you encapsulate the code you expect might raise an exception in a special statement, like below.
try {
// Do something here
}
Then, you handle the exception.
catch (Exception e) {
// Do something to gracefully fail
}
If you need certain things to execute regardless of whether an exception is raised, add finally
.
finally {
// Clean up operation
}
All together it looks like this.
try {
// Do something here
}
catch (AnotherException ex) {
}
catch (Exception e) { //Exception class should be at the end of catch hierarchy.
}
finally {
}

- 12,503
- 8
- 58
- 65

- 4,373
- 1
- 24
- 27
-
1can we catch the runtime exception? How it is possible? with out knowing the runtime exception, how we catch it programatically? – RKCY Jan 11 '10 at 19:02
-
You can use `catch (ExceptionType name) {}` to catch any type of exception. Otherwise, you can use `catch {}` to catch all exceptions, or `catch (Exception name) {}` to catch all exceptions (and have the information about them accessible to you. – Ed Altorfer Jan 11 '10 at 19:22
-
@Ed: It is correct.But if i got error on the server(loading issue).this is runtime exception.How we will catch this exception programatically? is it possible? – RKCY Jan 11 '10 at 19:49
-
I don't know what you mean...you catch exceptions programmatically using the syntax I gave you. – Ed Altorfer Jan 12 '10 at 01:36