Sunday, October 20, 2019
Three Types of Exceptions in Java
Three Types of Exceptions in Java Errors are the bane of users and programmers alike. Developers obviously dont want their programs falling over at every turn and users are now so used to having errors in programs that they grudgingly accept to pay the price for software that will almost certainly have at least one error in it. Java is designed to give the programmer a sporting chance in designing an error-free application. There are exceptions that the programmer will know are a possibility when an application interacts with a resource or a user and these exceptions can be handled. Unfortunately, there are exceptions the programmer cant control or simply overlooks. In short, all exceptions are not created equal and therefore there are several types for a programmer to think about. An exception is an event which causes the program to be unable to flow in its intended execution. There are three types of exception- the checked exception, the error and the runtime exception. The Checked Exception Checked exceptions are exceptions that a Java application should be able to cope with. For example, If an application reads data from a file it should be able to handle the FileNotFoundException . After all, there is no guarantee that the expected file is going to be where it is supposed to be. Anything could happen on the file system, which an application would have no clue about. To take this example one step further. Lets say we are using the FileReader class to read a character file. If you have a look at the FileReader constructor definition in the Java api you will see its method signature: public FileReader(String fileName) throws FileNotFoundException As you can see the constructor specifically states that the FileReader constructor can throw a FileNotFoundException. This makes sense as its highly likely that the fileName String will be wrong from time to time. Look at the following code: public static void main(String[] args){ FileReader fileInput null; //Open the input file fileInput new FileReader(Untitled.txt); } Syntactically the statements are correct but this code will never compile. The compiler knows the FileReader constructor can throw a FileNotFoundException and its up to the calling code to handle this exception. There are two choices - firstly we can pass the exception on from our method by specifying a throws clause too: public static void main(String[] args) throws FileNotFoundException{ FileReader fileInput null; //Open the input file fileInput new FileReader(Untitled.txt); } Or we can actually handle with the exception: public static void main(String[] args){ FileReader fileInput null; try { //Open the input file fileInput new FileReader(Untitled.txt); } catch(FileNotFoundException ex) { //tell the user to go and find the file } } Well-written Java applications should be able to cope with checked exceptions. Errors The second kind of exception is known as the error. When an exception occurs the JVM will create an exception object. These objects all derive from the Throwable class. The Throwable class has two main subclasses- Error and Exception. The Error class denotes an exception that an application is not likely to be able to deal with.Ã These exceptions are considered rare. For example, the JVM might run out of resources due to the hardware not being able to cope with all the processes it is having to deal with. Its possible for the application to catch the error to notify the user but typically the application is going to have to close until the underlying problem is dealt with. Runtime Exceptions A runtime exception occurs simply because the programmer has made a mistake. Youve written the code, it all looks good to the compiler and when you go to run the code, it falls over because it tried to access an element of an array that does not exist or a logic error caused a method to be called with a null value. Or any number of mistakes a programmer can make. But thats okay, we spot these exceptions by exhaustive testing, right? Errors and Runtime Exceptions fall into the category of unchecked exceptions.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.