Thursday, February 16, 2012

Runtime and checked exceptions

Java packages contain Exception subclasses that describe exceptions that are specific to each package. Each subclass of Exception represents a particular exception type.
The RuntimeException class - a subclass of Exception - and its subclasses describe exception objects that are thrown automatically by the Java Virtual Machine (JVM) at runtime.

Runtime exceptions are generally caused by bugs in the source code. For instance, you need to ensure that a divisor is not equal to zero before dividing by it.
The RuntimeException subclass contains various subclasses. These include
  • ArithmeticException
  • IndexOutOfBoundsException
  • IllegalStateException
  • NegativeArraySizeException
ArithmeticException
When a program tries to do something that breaks the rules of arithmetic, an ArithmeticException is thrown. For example, if a method tries to divide an integer by zero, the method throws an instance of this class.
IndexOutOfBoundsException
When an index to a string or an array is out of range, an IndexOutOfBoundsException is thrown. For example, trying to access the twelfth element of a ten element array will throw this exception.
IllegalStateException
When a method has been invoked illegally an IllegalStateException is thrown. In other words it was not in the correct state to be called at the time.
NegativeArraySizeException
If an application tries to create an array that has negative size, a NegativeArraySizeException is thrown. An array must have zero or more elements.
You can catch RuntimeException types by using try-catch blocks.
Code that might trigger a runtime exception, but that does not contain try-catch blocks, will still compile. All exceptions deriving from RuntimeException are known as unchecked exceptions. This means that the compiler does not check whether they are handled or declared.
All the classes that inherit from Exception and are not RuntimeException subclasses are known as checked exception classes.
When checked exceptions might occur in a method, they must either be declared in the method declaration using a throws clause, or explicitly handled in the method body. Otherwise, the code will not compile.

An example of a situation that can cause a checked exception is when a method attempts to open a file that cannot be opened.

You use a try-catch statement to handle checked exceptions that are raised.
Checked exception classes include
  • ClassNotFoundException
  • InterruptedException
  • IllegalAccessException
  • FileNotFoundException
ClassNotFoundException
ClassNotFoundException is thrown when a program tries to load a class using its name, but the definition for the class with the specified name is not found. These exceptions occur when a program uses the forName method in the Class class or the findSystemClass or loadClass method of the ClassLoader class.
InterruptedException
When a thread has been inactive for a long time and another thread uses the interrupt method in the Thread class to interrupt it, an InterruptedException is thrown.
IllegalAccessException
When the currently executing method does not have access to the definition of a field the application is trying to get or set, an IllegalAccessException is thrown. This also applies if the method doesn't have access to the definition of a method the application is trying to invoke.
FileNotFoundException
FileNotFoundException, which belongs to the java.io package, is thrown when an application fails to open a file specified by a pathname. This can happen if the file is inaccessible, or if it doesn't exist.

6 comments:

I do trust all of the concepts you’ve presented on your post. They’re really convincing and will definitely work. Still, the posts are too brief for newbies. May you please extend them a little from subsequent time?Also, I’ve shared your website in my social networks.
Office Interior Designers in Bangalore
Office Interior Designers in Hyderabad

It is really very excellent,I find all articles was amazing.Awesome way to get exert tips from everyone,not only i like that post all peoples like that post.Because of all given information was wonderful and it's very helpful for me.
SAP Training in Chennai
SAP ABAP Training in Chennai
SAP FICO Training in Chennai
SAP MM Training in Chennai

Hmm, it seems like your site yet my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.


apple ipad service center in chennai | apple iphone service center in chennai | iphone service center in chennai | | Apple laptop service center in chennai

Post a Comment