About Diagram for exception classes hierarchy Object, Throwable, Exception and Error in java programming code tutorials, video and example.



About bucket example for exceptions in Java programming

Video:

Object hierarchy in java programming for Exceptions:

Object 
          - Throwable
                            - Exception
                                             - IOException
                                                                  - EOFException
                                                                  - FileNotFoundException
                                                                  - - - 
                                             - SQLException
                                             - ClassNotFoundException
                                             - CloneNotSupportedException
                                             -  - - 
                                             - RunTimeException
                                                                             - NullPointerException
                                                                             - AirthmeticException 
                                                                             - - - 
                            - Error
                                     - VirtualMachinError
                                     - AssertionError  

Diagram for exceptions classes hierarchy:


About Diagram for exception classes hierarchy Object, Throwable, Exception and Error in java programming code tutorials, video and  example.


Video:


Note:
Checked exceptionAll exceptions are checked exceptions, except for those indicated by ErrorRuntimeException, and their subclasses.
Unchecked exceptionsErrors and runtime exceptions are collectively known as unchecked exceptions.

Code example:

Create the new java project in Eclipse.

Open the Eclipse
File > New > Java Project
Type the project name: Exceptions-In-Java-Programming
Click the next button
Click the Finish

Add the below java files:

Right click on the src folder > new > Class
Type the class package name: com.exceptions.tutorialbyexample
Type the Class name: CheckedExceptionCompileTime.java
package com.exceptions.tutorialbyexample;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class CheckedExceptionCompileTime {

public CheckedExceptionCompileTime() {
// TODO Auto-generated constructor stub
}

public void callMe() {

try {
System.out.println("Inside print method");
FileInputStream objFIS = null;
/**
* This constructor FileInputStream(File filename) throws
* FileNotFoundException which is a checked exception
*/
objFIS =
new FileInputStream("c:/tutorialbyexample-for-exception.txt");
int i;

/*
* Method read() of FileInputStream class also throws a checked
* exception: IOException
*/
while ((i = objFIS.read()) != -1) {
System.out.print((char) i);
}

/*
* The method close() closes the file input stream It throws
* IOException
*/
objFIS.close();

} catch (FileNotFoundException e) {
e.getMessage();
} catch (IOException e) {
e.getMessage();
}
}

public static void main(String[] args) {
// Creating the object of ExceptionsInJavaProgramming
CheckedExceptionCompileTime obj = new CheckedExceptionCompileTime();
System.out.println("Inside main method");
obj.callMe();
}

}

If not handled by try/cache  or Throws then, code will not compile it:

Unhandled exception type IOException
Unhandled exception type FileNotFoundException

Right click on the src folder > new > Class
Type the class package name: com.exceptions.tutorialbyexample
Type the Class name: UnCheckedExceptionOrRunTime.java

package com.exceptions.tutorialbyexample;

public class UnCheckedExceptionOrRunTime {

public UnCheckedExceptionOrRunTime() {
// TODO Auto-generated constructor stub
}

public void print() {
int a = 10, b = 0, c;
try {
c = a / b;
System.out.println("Value of c = " + c);
} catch (java.lang.ArithmeticException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
// Creating the object of ExceptionsInJavaProgramming
UnCheckedExceptionOrRunTime obj = new UnCheckedExceptionOrRunTime();
obj.print();
}
}

Right click on the src folder > new > Class
Type the class package name: com.exceptions.tutorialbyexample
Type the Class name: UnCheckedExceptionOrRunTimeWithTryCatchAndFinally.java

package com.exceptions.tutorialbyexample;

public class UnCheckedExceptionOrRunTimeWithTryCatchAndFinally {

public UnCheckedExceptionOrRunTimeWithTryCatchAndFinally() {
// TODO Auto-generated constructor stub
}

public void print() {
int a = 10, b = 0, c;
try {
c = a / b;
System.out.println("Value of c = " + c);
} catch (java.lang.ArithmeticException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Inside finally");
}
}

public void printExit() {
int a = 10, b = 0, c;
try {
// return "";
// System.exit(0);
c = a / b;
System.out.println("Value of c = " + c);

} catch (java.lang.ArithmeticException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Inside printExit finally");
}
// return null;
}

public static void main(String[] args) {
// Creating the object of ExceptionsInJavaProgramming
UnCheckedExceptionOrRunTimeWithTryCatchAndFinally obj
= new UnCheckedExceptionOrRunTimeWithTryCatchAndFinally();
obj.print();
obj.printExit();
}
}

Please like and share it if you like my tutorial and video!!!

Reference form:
Oracle.com and Sun java

1 comment: