Results 1 to 3 of 3
  1. #1
    Melssj5 is offline Member
    Join Date
    Mar 2011
    Rep Power

    Default Cant understand RuntimeException

    Hi Guys, please can you help me to understand the RuntimeException.

    Why doent it need to be captured when thrown or catched????

    I can throw it without adding the throws to the method and can call the method without surrounding it by a try, and can put it inside a try without having to catch it....

    Thanks to all!

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Richmond, Virginia
    Blog Entries
    Rep Power


    Runtime exceptions(unchecked) are generally used for programmer errors. Think of the type of things that cause runtime exceptions(array index out of bounds, casting errors, null pointer exception) These errors aren't intended to be caught and handled by the user, since they are really designed to point out an error in logic.

    As for the other exceptions(checked) they give the client the chance to handle some exceptional situation(non existant file, bad location in a document.

    I suggest you read up on exceptions if you aren't totally familiar on them. The sun tutorials are bound to be a great resource,

    Lesson: Exceptions (The Java™ Tutorials > Essential Classes)

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Rep Power


    A RuntimeException generally represents a situation where your program cannot/ should not recover from. It can highlight a serious problem in your code and should be fixed not just ignored as a lot of exception handling does.

    A classic example is an ArrayIndexOutOfBoundsException. This is thrown when your code attempts to access an element that does not exist in the array. If you were able to catch and continue on in this situation what should happen? A more important question is why is it accessing a non-existant element in the first place? So instead of catching the exception and have the program go on its merry way thinking all is right in the world. A RuntimeException is thrown and you must fix it.

    Of course there are always exceptions to the rule. What you can do in some situations is to make sure your program is in the correct state before attempting to do things. More examples: a NullPointerException and a ArithmeticException(divide by zero). In both cases you can use an if statement to avoid these exceptions.
    Java Code:
    Foo f = findFoo(someValue); // method might return null
    if(f != null) {
    double number = getUserInput();
    double divisor = getUserInput(); // user may enter 0
    if(divisor == 0) {
        print error message
    } else {
        result = number / divisor;

Similar Threads

  1. Replies: 3
    Last Post: 03-21-2011, 02:48 PM
  2. Replies: 2
    Last Post: 10-30-2009, 12:55 PM
  3. Replies: 0
    Last Post: 07-06-2009, 02:38 PM
  4. Replies: 4
    Last Post: 06-24-2009, 06:34 AM
  5. java.lang.RuntimeException+ Axis
    By Peter in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 01:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts