0% found this document useful (0 votes)
18 views25 pages

EELU-OOP-Week6 - Exceptions

OOP Exceptions

Uploaded by

ak7055864
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views25 pages

EELU-OOP-Week6 - Exceptions

OOP Exceptions

Uploaded by

ak7055864
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

By

Dr. Yasser Abdelhamid


 Exceptions
 Anexception is an event that disrupts the
normal flow of the program.
 An Exception is an unwanted event that
interrupts the normal flow of the program.
 When an exception occurs program
execution gets terminated.
 In such cases we get a system generated
error message.
 By handling the exceptions we can
provide a meaningful message to the user
about the issue rather than a system
generated message, which may not be
understandable to a user.
 There can be several reasons that can
cause a program to throw exception.
 For example:
 Opening a non-existing file in your program,
 Network connection problem,
 Bad input data provided by user etc.
 Ifan exception occurs, which has not
been handled by programmer then
program execution gets terminated and a
system generated error message is shown
to the user.
 Example:

Exception in thread "main" java.lang.ArithmeticException: / by zero at


ExceptionDemo.main(ExceptionDemo.java:5)
ExceptionDemo : The class name
main : The method name
ExceptionDemo.java : The filename
java:5 : Line number

 Instead of presenting this message, we can display a friendly


message to the user specifying what went wrong, and giving
him the chance to continue running the program without
break.
 ExceptionHandling is a mechanism to
handle runtime errors so that when an
exception occurs, the program can still go
back to the normal flow.
java.lang.Throwable class is
the root class of Java
Exception hierarchy inherited
by two subclasses:
 Exception and
 Error.
 Errorsindicate that something severe
enough has gone wrong, the application
should crash rather than try to handle the
error.

 Exceptionsare events that occurs in the


code. A programmer can handle such
conditions and take necessary corrective
actions.
 All exceptions other than Runtime Exceptions are
known as Checked exceptions.
 The compiler checks them during compilation to see
whether the programmer has handled them or not.
 If these exceptions are not handled/declared in the
program, you will get compilation error.
 For example:
 SQLException, IOException, ClassNotFoundException etc.
 Runtime Exceptions are also known as Unchecked
Exceptions.
 These exceptions are not checked at compile-time
so compiler does not check whether the
programmer has handled them or not but it’s the
responsibility of the programmer to handle these
exceptions and provide a safe exit.
 For example:
 ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException etc.
 Exceptions
are handled in java using Try-
Catch code blocks.
 The try block contains set of statements
where an exception might occur.
 A try block is always followed by a catch
block, which handles the exception that
occurs in associated try block.
 A try block must be followed by catch
blocks or finally block or both.

try{
//statements that may cause an exception
}
 A catch block is where you handle the
exceptions.
 This block must follow the try block.
 A single try block can have several catch
blocks associated with it.
 When an exception occurs in try block, the
corresponding catch block that handles that
particular exception executes.

try
{
//statements that may cause an exception
}
catch (exception(type) e(object))
{
//error handling code
}
 The generic exception handler can handle
all the exceptions but you should place it
at the end of catch blocks.

 Ifyou place it before all the catch blocks


then it will display its generic message.
which may not be meaningful to the user.
class Example1 {
public static void main(String args[]) {
int num1, num2;
try {
/* We suspect that this block of statement can throw exception */
num2 = 62 / num1;
System.out.println(num2);
System.out.println("Hey I'm at the end of try block");
}
catch (ArithmeticException e) {
/* This block will only execute if any Arithmetic exception occurs in try block */
System.out.println("You should not divide a number by zero");
}
catch (Exception e) {
/* This is a generic Exception handler which means it can handle all the exceptions.
This will execute if the exception is not handled by previous catch blocks .*/
System.out.println("Exception occurred");
}
System.out.println("out of try-catch block");
}
}

Output:
You should not divide a number by zero
Out of try-catch block...
class Example{
public static void main(String args[]){
try{
int arr[]=new int[7];
arr[4]=30/0; // divide by zero
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:
You should not divide a number by zero
Out of the try-catch block
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
class Nest{
public static void main(String args[]){
//Parent try block
try{
//Child try block1
try{
System.out.println("Inside block1");
int b =45/0;
System.out.println(b);
}
catch(ArithmeticException e1){
System.out.println("Exception: e1");
}
//Child try block2
try{
System.out.println("Inside block2");
int b =45/0;
System.out.println(b);
}
catch(ArrayIndexOutOfBoundsException e2){
System.out.println("Exception: e2");
}
System.out.println("Just other statement");
}
catch(ArithmeticException e3){
System.out.println("Arithmetic Exception");
System.out.println("Inside parent try catch block");
}
catch(ArrayIndexOutOfBoundsException e4){
System.out.println("ArrayIndexOutOfBoundsException");
System.out.println("Inside parent try catch block");
}
catch(Exception e5){
System.out.println("Exception");
System.out.println("Inside parent try catch block");
}
System.out.println("Next statement..");
}
}
A finally block contains all the crucial
statements that must be executed
whether exception occurs or not.
 The statements present in this block will
always execute regardless of whether
exception occurs in try block or not such
as closing a connection, stream, file, etc.
class Example
{
public static void main(String args[]) {
try{
int num=121/0;
System.out.println(num);
}
catch(ArithmeticException e){
System.out.println("Number should not be divided by zero");
}
/* Finally block will always execute
* even if there is no exception in try block
*/
finally{
System.out.println("This is finally block");
}
System.out.println("Out of try-catch-finally");
}
Output:
} Number should not be divided by zero
This is finally block
Out of try-catch-finally
class JavaFinally
{
public static void main(String args[])
{
System.out.println(JavaFinally.myMethod());
}
public static int myMethod()
{
try {
return 112;
}
finally {
System.out.println("This is Finally block");
System.out.println("Finally block ran even after return statement");
}
}
}
Output:
This is Finally block
Finally block ran even after return statement
112

You might also like