Checked and unchecked exceptions:
We have seen that an exception in Java is an object and that there can be different types of exceptions for different situations. Figure shows part of the hierarchy of Java exception classes.
The root of the exception inheritance tree is the class Throwable. This represents the most general class for describing exceptions.
The subclasses of Throwable can be divided into three main groups:
- the Error class and its subclasses;
- the Exception class and its subclasses, excluding the RunTimeException class and its subclasses;
- the RunTimeException class and its subclasses.
The error conditions corresponding to each of these three groups normally have different underlying causes and are treated differently by the Java system.
The class Error describes exceptions that occur when some internal Java error has happened - for example, the Java system has run out of memory. Such errors are rare and there is little that a programmer can do about them. The Java system monitors them and terminates the program, displaying some indication of what has happened.
Errors that are described by the class Exception and its subclasses can be monitored and acted on. Some of these are defined as checked exceptions. As you will see later in this unit, programmers must include code to declare or handle any checked exceptions that might occur. The Java compiler will report an error if this has not happened.
Checked exceptions include all exceptions of type Exception and of any subclasses of Exception, other than RunTimeException and its subclasses. Exceptions not covered by this definition are known, unsurprisingly, as unchecked exceptions.
Java contains facilities for catching the exceptions that can occur during the running of a program. However, it is normally worth catching only exceptions relating to checked exception types. As we have seen, system errors described by the Error class are almost invariably fatal. Exceptions of type RunTimeException or its subclasses are normally due to programming errors - these should be eradicated by proper design, good programming style and exhaustive testing.
There are many built-in exception classes provided by the Java class library. These cover the most important errors that normally can occur within a Java program, but you can also define your own exception classes, as we show in a later section.
The following are examples of checked exceptions.
- EOFException occurs when a program attempts to read past the end of a file.
- FileNotFoundException can occur when a program attempts to open or write to an existing file. If the file cannot be found, then this exception is thrown.
- MalformedURLException indicates that an invalid form of URL (such as a website address) has occurred.
These are checked exceptions because they are subclasses of IOException, which is a direct subclass of Exception.
The following are examples of unchecked exceptions, as they are subclasses of RunTime Exception.
- ArrayIndexOutOfBoundsException occurs when a program tries to access an array with an index outside the valid limits.
- ArithmeticException arises when an illegal arithmetic condition occurs, such as an attempt to divide an integer by zero.
- NumberFormatException can be caused by an application expecting to read a number of some sort, when the data does not have the appropriate format.
- NullPointerException happens when an application attempts to use null in a case where an object is required: for example, invoking a method on a null reference.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Checked and unchecked exceptions homework and assignments? Live Checked and unchecked exceptions experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Checked and unchecked exceptions homework help, java assignment help and Checked and unchecked exceptions projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours