Lecture 8

Lecture 8

-What is Exception?

-Why we handle them

-Exception Major types

-Try catch finally block


The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.

In this page, we will learn about Java exceptions, its type and the difference between checked and unchecked exceptions.

What is Exception in Java

Dictionary Meaning: Exception is an abnormal condition.

In Java, an exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.

What is Exception Handling

Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc.

Advantage of Exception Handling

The core advantage of exception handling is to maintain the normal flow of the application. An exception normally disrupts the normal flow of the application that is why we use exception handling. Let’s take a scenario:

  1. statement 1;
  2. statement 2;
  3. statement 3;
  4. statement 4;
  5. statement 5;//exception occurs
  6. statement 6;
  7. statement 7;
  8. statement 8;
  9. statement 9;
  10. statement 10;

Suppose there are 10 statements in your program and there occurs an exception at statement 5, the rest of the code will not be executed i.e. statement 6 to 10 will not be executed. If we perform exception handling, the rest of the statement will be executed. That is why we use exception handling in Java.

Do You Know?

o    What is the difference between checked and unchecked exceptions?

o    What happens behind the code int data=50/0;?

o    Why use multiple catch block?

o    Is there any possibility when finally block is not executed?

o    What is exception propagation?

o    What is the difference between throw and throws keyword?

o    What are the 4 rules for using exception handling with method overriding?

Hierarchy of Java Exception classes

The java.lang.Throwable class is the root class of Java Exception hierarchy which is inherited by two subclasses: Exception and Error. A hierarchy of Java Exception classes are given below:

Types of Java Exceptions

There are mainly two types of exceptions: checked and unchecked. Here, an error is considered as the unchecked exception. According to Oracle, there are three types of exceptions:

  1. Checked Exception
  2. Unchecked Exception
  3. Error

Difference between Checked and Unchecked Exceptions

1) Checked Exception

The classes which directly inherit Throwable class except RuntimeException and Error are known as checked exceptions e.g. IOException, SQLException etc. Checked exceptions are checked at compile-time.

2) Unchecked Exception

The classes which inherit RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time, but they are checked at runtime.

3) Error

Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.

Java Exception Keywords

There are 5 keywords which are used in handling exceptions in Java.

Keyword Description
try The “try” keyword is used to specify a block where we should place exception code. The try block must be followed by either catch or finally. It means, we can’t use try block alone.
catch The “catch” block is used to handle the exception. It must be preceded by try block which means we can’t use catch block alone. It can be followed by finally block later.
finally The “finally” block is used to execute the important code of the program. It is executed whether an exception is handled or not.
throw The “throw” keyword is used to throw an exception.
throws The “throws” keyword is used to declare exceptions. It doesn’t throw an exception. It specifies that there may occur an exception in the method. It is always used with method signature.

Java Exception Handling Example

Let’s see an example of Java Exception Handling where we using a try-catch statement to handle the exception.

  1. publicclass JavaExceptionExample{
  2. public static void main(String args[]){
  3. try{
  4. //code that may raise exception
  5. int data=100/0;
  6. }catch(ArithmeticException e){System.out.println(e);}
  7. //rest code of the program
  8. out.println(“rest of the code…”);
  9. }
  10. }

Test it Now



Exception in thread main java.lang.ArithmeticException:/ by zerorest of the code…

In the above example, 100/0 raises an ArithmeticException which is handled by a try-catch block.

Common Scenarios of Java Exceptions

There are given some scenarios where unchecked exceptions may occur. They are as follows:

1) A scenario where ArithmeticException occurs

If we divide any number by zero, there occurs an ArithmeticException.

  1. inta=50/0;//ArithmeticException

2) A scenario where NullPointerException occurs

If we have a null value in any variable, performing any operation on the variable throws a NullPointerException.

  1. String s=null;
  2. out.println(s.length());//NullPointerException

3) A scenario where NumberFormatException occurs

The wrong formatting of any value may occur NumberFormatException. Suppose I have a string variable that has characters, converting this variable into digit will occur NumberFormatException.

  1. String s=”abc”;
  2. inti=Integer.parseInt(s);//NumberFormatException

4) A scenario where ArrayIndexOutOfBoundsException occurs

If you are inserting any value in the wrong index, it would result in ArrayIndexOutOfBoundsException as shown below:

  1. inta[]=new int[5];
  2. a[10]=50; //ArrayIndexOutOfBoundsException











Extra Topic to be covered for future Work





  1. Java not pure object oriented bcoz of the int, float etc mean primitive data type.
  2. String have a class but all other primitive data type have no class that is why java is called 99 percent object oriented language but not 100%.
  3. For long integer and value we take long and short int
  4. Double and float is used for decimal value
  5. Default value of java is double in decimal value case
  6. Write a program of int, long , once keep value of int x=65666997 e.g (int x=656569568;


Java have three type of opretar

1 uniry opretor   ++,–nincrement decrement

  1. Operators arithemic operators +,-,*,/,% g

public class calculatedemo {

public static void main(String[] args) {

char ch=65;

char  cha=’’A;


  1. String is treated as array e.g.,

public static void main(String[] args) {

String st=”Ali”;



  1. Another example public static void main(String[] args) {

String st=new String (“Ali”);





Wraper Class

Integer  is integer class and type of primitive data type.


Integer x=new Integer(1);


If u want to see the class press ctrl+click.

  • If we have double its also primitive data type but Double is class.
  • Integer, Byte all with capital starting letter are called wraper class can be use for different functionalities.
  • /* String value1=”111″;
  • String value2=”222″;
  • out.print(value1+value2);*/


  1. int x=2.3 will print 2 value
  2. declaring float y=2.3f


Abstract Sentence tree (AST)

All patterns are defined before to do programming.

Line by line execution.




Type casting

Float abc=11.2f or

Float abc=(float)11.2

2 types implicit and explicit

Implicit:- done by JVM


double a=5.5




Explicit: done by programmer-

Long x=5;

Int y=(int) x;


Short z=(short) y;

Print value of y


Unboxing and auto boxing

Unboxing the primitive converted to wrapper

int i=10;           //autoboxing

Integer abc=new Integer(i);

System.out.println(“value of long=”+abc);


Autoboxing the wrapper is converted to primitive

int i=10;           //unboxing

Integer abc=new Integer(i);

int xyz=abc;        // autoboxing

System.out.println(“value of long=”+abc);

System.out.println(“value of long=”+xyz);

Previous Lecture

Next Lecture

Lecture No

1     2     3     4     5     6     7     8     9     10     11     12     13     14     15

Please follow and like us:

Author: ilearnpoint

1 thought on “Lecture 8

Leave a Reply

Your email address will not be published. Required fields are marked *