You are on page 1of 27

Discovering Knowledge

CSC-210
Object-Oriented Programming

Lecturer
Sameena Javaid

https://sites.google.com/site/sameenajavaidcs

© Bahria University Department of Computer Science | Bahria University 1


Discovering Knowledge

EXCEPTIONS

© Bahria University Department of Computer Science | Bahria University 2


Discovering Knowledge

A Little Demo
public class Test {
public static void main(String[] args) {
int i = 6;
int j = 3;
System.out.println(i/j);
}
}
Output :
2

© Bahria University Department of Computer Science | Bahria University 3


Discovering Knowledge

A Little Demo
public class Test {
public static void main(String[] args) {
int i = 6;
int j = 0;
System.out.println(i/j);
}
}
Output :
Exception in thread "main"
java.lang.ArithmeticException: / by zero
at Test.main(Test.java:4)

© Bahria University Department of Computer Science | Bahria University 4


Discovering Knowledge

What is Exception Handling?


• Exception is the one that stops the execution
of the program unexpectedly.
• The process of handling these exceptions is
called Exception Handling.

© Bahria University Department of Computer Science | Bahria University 5


Discovering Knowledge

Exception Handling Mechanism


Exception can be handled in 3 ways:
• try block
• Catch block
• Finally block

© Bahria University Department of Computer Science | Bahria University 6


Discovering Knowledge

Exception Classes
Object

Throwable Exception Compile – enforced


Exception

2
Error Runtime
Exceptions

© Bahria University Department of Computer Science | Bahria University 7


Discovering Knowledge

The Hierarchy of Exception Classes

© Bahria University Department of Computer Science | Bahria University 8


Discovering Knowledge

try Try and Catch block


{
//code where you think exception would occur
}

catch(Exception_Class reference)
{
//Catch the exception and displays that
exception
}

© Bahria University Department of Computer Science | Bahria University 9


Discovering Knowledge

Try – Catch example


public class Try_Catch {
public static void main(String[] args) {
int y=0;
try {
System.out.println(5/y);
}
catch(Exception e) {
System.out.println(“Divide By Zero Exception”);
}
}
}

© Bahria University Department of Computer Science | Bahria University 10


Discovering Knowledge

Multiple Catches
Try {
• When there is a //statements
chance of getting }
different types of
catch(Exception_Class reference) {
exceptions we use //statements for one type of
multiple catch block exception
for a try block. }
catch(Exception_Class reference) {
//statements for other type of
exception
}

© Bahria University Department of Computer Science | Bahria University 11


Discovering Knowledge

class Multiple_Catch {
Multiple-Catch Example
catch(ArrayIndexOutOfBoundsException
arrayexception)
int n; {
int array[]=new int[3]; System.out.println(arrayexception);
Multiple_Catch(int n) }
{
try{ catch(ArithmeticException divideexception)
if(n==0) {
System.out.println(divideexception);
System.out.println(5/n);
}
else{
}
array[3]=n;
}//class end
System.out.println(array);
}
}
© Bahria University Department of Computer Science | Bahria University 12
Discovering Knowledge

Multiple- Catch Example


class Main {

public static void main(String[] args)


{

Multiple_Catch multiplecatch1= new Multiple_Catch(0);


Multiple_Catch multiplecatch2= new Multiple_Catch(5);

}
}

© Bahria University Department of Computer Science | Bahria University 13


Discovering Knowledge

What is throw keyword?


• throw is a keyword which is used to call the sub class
of an exception class.

• This keyword is also used to throw the exception


occurred in try block to catch block.

try{
throw new Exception_class(“message”);
}
catch(Exception_class reference){
//statements
}

© Bahria University Department of Computer Science | Bahria University 14


Discovering Knowledge

Example using throw keyword


public class Student {
public class Main {
Student(int studentid, String name){
public static void main(String[] args)
{
try{
if(studentid==0)
Student student1 =
throw new Exception("id can not
be zero"); new Student(0,"STUDENT1");
else Student student2 =
System.out.println("The id of new Student(1,"STUDENT2");
"+name+"
is:"+studentid); }
}
catch (Exception e) { }
System.out.println(e);
}
}
}

© Bahria University Department of Computer Science | Bahria University 15


Discovering Knowledge

What is throws keyword?


• throws is a keyword applied to methods for
which an exception has raised during its
execution.

returntype method_name throws Exception_Class


{
// statements
}

© Bahria University Department of Computer Science | Bahria University 16


Discovering Knowledge

Example using throws keyword


public class Exception2 {
public static int getInput() throws
public static void main(String[] args) {
Exception {
int x =0, total =0;
while (x >=0){
Scanner input = new
total+=x; Scanner(System.in);
try {
int a = input.nextInt();
System.out.println("Enter a +ve number to continue"); return a;
x = getInput(); }
} }// class Exception2 Ends here
catch (Exception e){
System.out.println(e);
x=0;//reset to zero (otherwise incorrect result)
}
}
System.out.println("Total is ="+total);
}// main end here

© Bahria University Department of Computer Science | Bahria University 17


Discovering Knowledge

Uses of finally keyword


• When we want a set of statements to be executed
even after an exception has occurred then we use
finally block.
• finally
{
//statements that needs to be executed after
exception
}

© Bahria University Department of Computer Science | Bahria University 18


Discovering Knowledge

Types of Exception
• Run-time Exceptions.
• Compile Enforced Exception

© Bahria University Department of Computer Science | Bahria University 19


Discovering Knowledge

Run-Time Exceptions
• Are also called as Unchecked Exception.
• These exceptions are handled at run-time i.e by JVM
after they have occurred by using try and catch
block.
• Eg: ArrayIndexOutOfBoundsException,
ArithmeticException
NullPointerException

© Bahria University Department of Computer Science | Bahria University 20


Discovering Knowledge

Complier-enforced Exceptions
• Are also called as Checked Exceptions.
• These exceptions are handled by java complier
before they occur by using throws keyword.
• Eg: IOException,
FileNotFoundException

© Bahria University Department of Computer Science | Bahria University 21


Discovering Knowledge

User-defined Exceptions
• Across built-in exceptions user can also
define his own exceptions.
• It can be done by defining a class that extends
Exception class and creating a constructor of
the class (user-defined) with string argument
to print that message when exception occurs.

© Bahria University Department of Computer Science | Bahria University 22


Discovering Knowledge

User-defined Exceptions

© Bahria University Department of Computer Science | Bahria University 23


Discovering Knowledge

User-defined Exceptions (Example)


import java.lang.Exception; class Lab5 {
class LessThanException extends Exception { public static void main(String args[]) {
LessThanException(String message) { int a=5;
System.out.println(message);
int b=3;
}
}
try {
int c=a*b;
if(c<50) {
throw new LessThanException("The product of the
number is less than 50");
}
}
catch(LessThanException lessExp) {
System.out.println("Caught my exception");
}
System.out.println(a*b);
} //main ends
} //class ends

© Bahria University Department of Computer Science | Bahria University 24


Discovering Knowledge

Advantages of Exception
• The program will still execute even if an
exception arises i.e finally block.
• If you can't handle the exception JVM will
handle the exception if we use throws
keyword.
• We can differentiate the exceptions that have
occurred.

© Bahria University Department of Computer Science | Bahria University 25


Discovering Knowledge

Errors and Error Handling


Design-time error: These are the errors that
occur while designing the programs.
Eg: Syntax errors

These errors will be shown with a red mark in


eclipse IDE so that you can easily find and
correct it.

© Bahria University Department of Computer Science | Bahria University 26


Discovering Knowledge

Errors and Error Handling


 Logical error: These are the errors done by
programmer. The programs with these errors will run
but does not produce desired results.
Eg: getting division of two numbers as output but
expected is multiplication of numbers.

These errors can be rectified by understanding the


logic and checking whether it is works out correctly
or not.

© Bahria University Department of Computer Science | Bahria University 27

You might also like