keyboard_arrow_up


An exception is a problem that occurs during the performance of a system. When an Exception happens the regular flow of the system is disturbed and the program/Application ends unusually, which is not suggested, therefore these exclusions are to be handled.




An exception can take place for many different reasons, below given are some circumstances where exception happens.




A customer has given incorrect information.




A file that needs to be opened out cannot be found.




A system connection has been missing at the center of emails or the JVM has run out of storage.




Some of these exclusions are due to customer mistake, others by developer mistake, and others by actual physical sources that didn't work in some way.




Based on these we have three groups of Exceptions you need to understand them to know how exception managing works in Java,




Examined exceptions: A checked exception is an exception that happens at the gather time, these are also known as as gather time exclusions. These exclusions cannot simply be ignored right at that moment of collection, the Programmer should take care of (handle) these exclusions.




import java.io.File;


import java.io.FileReader;




public class FilenotFound_Demo {




   public static void main(String args[]){      
       


      File file=new File("E://file.txt");


      FileReader fr = new FileReader(file); 


   }


   


}


compilation of the above program will provide you exceptions as shown below.


C:\>javac FilenotFound_Demo.java


FilenotFound_Demo.java:8: error: unreported
exception FileNotFoundException; must be caught or declared to be
thrown


      FileReader fr = new FileReader(file);


                      ^


1 error


Note: Since the techniques read() and close() of FileReader category brings IOException, you can notice that compiler informs to deal with IOException, along with FileNotFoundException.




Uncontrolled exceptions: An Uncontrolled exception is an exception that happens right at that moment of performance, these are also known as as Playback Exceptions, these consist of development insects, such as reasoning mistakes or inappropriate use of an API. runtime exclusions are ignored right at that moment of collection.






public class Unchecked_Demo {


   


   public static void main(String args[]){


      int num[]={1,2,3,4};


      System.out.println(num[5]);


   }




}


after compilation of the above code you will get the result as shown below


Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 5


        at
Exceptions.Unchecked_Demo.main(Unchecked_Demo.java:8)




Exception Hierarchy:




All exception is subtypes of the java.lang.Exception class. The exception category is a subclass of the Throwable class. Other than the exception class there is another subclass known as Error which comes from the Throwable class.




Errors are irregular circumstances which occur in case of serious problems, these are not managed by the java applications. Errors are produced to indicate mistakes produced by the runtime atmosphere. Example : JVM is out of Memory. Normally applications cannot restore from mistakes.




The Exception class has two main subclasses: IOException category and RuntimeException Class.




Exception Methods:




Public String get Message()


Returns a detailed concept about the exception that has happened. This concept is initialized in the Throwable construtor




Public Throwable getclause()


Clause is returned of the exception as showed by a Throwable item.




public String toString()


Returns the class name concatenated with an outcome of getMessage()




public void printStackTrace()


Prints a outcome of toString() along with the collection track to System.err, the big mistake outcome flow.




public StackTraceElement [] getStackTrace()


Returns an range containing each factor on the collection track. The factor at catalog 0 symbolizes the top of the decision collection, and the last look at the range symbolizes the method at the bottom of the decision collection.




public Throwable fillInStackTrace()


Fills the collection track of this Throwable item with the current collection track, adding to any previous information in the collection track. You can go through the java tutorial to know more about Java programming.