You are on page 1of 8

PORTFOLIO ON

EXCEPTION HANDLING

SUBMITTED BY
INSTRUCTOR NAME
MD.MONIRUL ISLAM
MEHRAB ZAMAN CHOWDHURY
ID : 221 002 154

EXCEPTION
An Exception is a run time error.

An Exception is an abnormal condition that arises in a code sequence at run time.

Example

int x=10;

int y=0;

int result = x/y // Exception occurs

MAJOR REASONS WHY AN EXCEPTION OCCURS

❖ Invalid user input


❖ Device failure
❖ Loss of Network connection
❖ Physical limitations ( out of disk memory )
❖ Code errors
❖ Opening an unavailable file
DIFFERENCE BETWEEN ERROR AND EXCEPTION
Errors indicate that something severe enough has gone wrong, the application should crash
rather than try to handle the error.
Exceptions are events that occurs in the code. A programmer can handle such conditions and
take necessary corrective actions.

DIFFERENT TYPE OF EXCEPTION


i) ArithmeticException
ii) NullPointException
iii) StringIndexOutOfBoundsException
iv) NumberFormatException
v) FileNotFoundException
vi) ArrayIndexOutOfBoundsException

ArithmeticException

int x=10;

int y=0;

int result = x/y // ArithmeticException

//Can not divide a number by 0

NullPointException

String name=null;

System.out.println(name.charAt(0));

// NullPointException
StringIndexOutOfBoundsException

String name=”OOP”;

System.out.println(name.charAt(5));

// StringIndexOutOfBoundsException

NumberFormatException

int name=”GUB”;

int num = Integer.parseInt(“GUB”);

// NumberFormatException

FileNotFoundException

File file = new File(“D://file.txt);

// FileNotFoundException
ArrayIndexOutOfBoundsException

Int A [ ] =new int [10 ];

A [10] = 100;

// ArrayIndexOutOfBoundsException

EXCEPTION HANDLING
The Exception handling in one of the powerful mechanisms to handle the runtime errors.

EXCEPTION HANDLING IS MANAGED BY 5 KEYWORDS


i) try
ii) catch
iii) finally
iv) throw
v) throws

try catch finally block

try {

// code you want to monitor

} catch ( ExceptionType obj ) {

// exception handler for exception

finally {

// block of the code to be executed after try block

}
Write a Program that will safely divide one integer by another integer. See the sample output
below. Sample execution :

Please Enter Num1 : 100

import java.util.Scanner; Please Enter Num 2 : 40

public class exceptionProblem{ Result : 100/40 = 2

public static void main(String [] args ){

while(true){ Please Enter Num1 : 100


try{ Please Enter Num 2 : hi

Scanner input=new Scanner(System.in);

System.out.println("Enter Num1 : ");

int num1=input.nextInt();
Please Enter Num1 : 100
System.out.println("Enter Num2 : ");
Please Enter Num 2 : 0
int num2=input.nextInt();
You Must enter integer.

Please try again.


int result=num1/num2;

System.out.println("Result = "+num1 +"/ "


+num2 +"=" +result );

catch(Exception e)

System.out.println("Exception" +e);

System.out.println("You Must enter integer");

}} }}
ANOTHER EXAMPLE

class Example{

public static void main(String args[]){

try{

int arr[]=new int[7];

arr[10]=10/5;

System.out.println("Last Statement of try block");

catch(ArithmeticException e){

System.out.println("You should not divide a number by zero");

catch(ArrayIndexOutOfBoundsException e){

System.out.println("Accessing array elements outside of the


limit");

catch(Exception e){

System.out.println("Some Other Exception");

System.out.println("Out of the try-catch block");

}
OUTPUT

Accessing array elements outside of the limit

Out of the try-catch block


ADVANTAGE OF EXCEPTION HANDLING
Exception handling ensures that the flow of the program doesn’t break when an exception occurs.
For example, if a program has bunch of statements and an exception occurs mid way after
executing certain statements then the statements after the exception will not execute and the
program will terminate abruptly. By handling we make sure that all the statements execute and
the flow of program doesn’t break.

In conclusion, exception handling is a crucial aspect of Java programming that allows developers to
handle runtime errors in a controlled and graceful manner. A well-structured and effective
exception handling portfolio can demonstrate your expertise in this area and showcase your ability
to handle exceptions, improve application stability, and ensure reliable code execution.

You might also like