concept runtime exception in category java

appears as: runtime exception, runtime exceptions, runtime exceptions, A runtime exception, runtime exception
Java SE 11 Programmer I Certification Guide MEAP V03

This is an excerpt from Manning's book Java SE 11 Programmer I Certification Guide MEAP V03.

Note As a quick introduction, a runtime exception is a programming error determined by the Java Runtime Environment (JRE) during the execution of code. These errors occur because of the inappropriate use of a piece of code (exceptions are covered in detail in chapter 12). The previous code tries to access a nonexistent index position, so it causes an exception.

Figure 12.8 Categories of exceptions: checked exceptions, runtime exceptions, and errors

Note Runtime exceptions and errors are collectively referred to as unchecked exceptions.

Of these three types, checked exceptions require most of your attention when it comes to coding and using methods. Runtime exceptions represent programming errors. Checks should be inserted to prevent runtime exceptions from being thrown. There are few options you can use for the errors, because they’re thrown by the JVM.

  • A runtime exception is a representation of a programming error. These occur from inappropriate use of a piece of code. For example, NullPointerException is a runtime exception that occurs when a piece of code tries to execute some code on a variable that hasn’t been assigned an object and points to null. Another example is ArrayIndexOutOfBoundsException, which is thrown when a piece of code tries to access an array element at a nonexistent position.
  • A runtime exception is named so because it isn’t feasible to determine whether a method call will throw a runtime exception until it executes.
  • A runtime exception is a subclass of java.lang.RuntimeException.
  • It’s optional to declare a runtime exception in the signature of a method. It’s up to the person who writes the code to decide whether to declare it explicitly or not.
  • The exam might trick you by including the names of runtime exceptions and errors in one method’s declaration and leaving them out in another. (You can include the name of unchecked exceptions in the throws clause, but you don’t have to.) Assuming that the rest of the code remains the same, the following method declaration is correct:

    public void readFile(String file)
                    throws NullPointerException, FileNotFoundException {  #A
        //rest of the code remains same
    }

    Exam Tip Adding runtime exceptions or errors to a method’s declaration isn’t required. A method can throw a runtime exception or error irrespective of whether its name is included in its throws clause.

    OCA Java SE 8 Programmer I Certification Guide

    This is an excerpt from Manning's book OCA Java SE 8 Programmer I Certification Guide.

    You can optionally assign the value returned by a method to a variable. If you don’t assign the returned value from a method, it’s neither a compilation error nor a runtime exception.

    Figure 7.8. Categories of exceptions: checked exceptions, runtime exceptions, and errors
  • A runtime exception is a representation of a programming error. These occur from inappropriate use of a piece of code. For example, NullPointerException is a runtime exception that occurs when a piece of code tries to execute some code on a variable that hasn’t been assigned an object and points to null. Another example is ArrayIndexOutOfBoundsException, which is thrown when a piece of code tries to access an array element at a nonexistent position.
  • A runtime exception is named so because it isn’t feasible to determine whether a method call will throw a runtime exception until it executes.
  • A runtime exception is a subclass of java.lang.RuntimeException.
  • It’s optional to declare a runtime exception in the signature of a method. It’s up to the person who writes the code to decide whether to declare it explicitly or not.
  • Derived
    Base
  • 7.3.3. Creating a method that throws runtime exceptions or errors

    When creating a method that throws a runtime exception or error, including the exception or error name in the throws clause isn’t required. A method that throws a runtime exception or error isn’t subject to the handle-or-declare rule.

    sitemap

    Unable to load book!

    The book could not be loaded.

    (try again in a couple of minutes)

    manning.com homepage