0% found this document useful (0 votes)
44 views

Exception in Java: by Montadher Issam Supervise Dr. Ali Kalakech

Uploaded by

mon_aladdab
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Exception in Java: by Montadher Issam Supervise Dr. Ali Kalakech

Uploaded by

mon_aladdab
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Islamic university

In Lebanon

EXCEPTION IN JAVA
By
Montadher issam
Supervise
Dr. Ali Kalakech

12/08/2021
OVERVIEW
 What is Exception?
 Type of Exception
 Exception Constructors
 General Format
 Exception handle
 Creating Custom Exception class
 Application 1
 Application 2
 Reference
OVERVIEW
 What is Exception?
 Type of Exception
 Exception Constructors
 General Format
 Exception handle
 Creating Custom Exception class
 Application 1
 Application 2
 Reference
WHAT IS EXCEPTION
 An exception is an indication of a problem
that occurs during a program's execution.
 Exception handling enables programmers
to remove error-handling code from the
"main line" of the program's execution,
improving program clarity and
enhancing modifiability.
WHAT IS EXCEPTION…
 Exceptions are thrown when a method
detects a problem and is unable to handle
it.
 The default java.lang package provides
several exception classes, all sub-classing
the RuntimeException class.
…WHAT IS EXCEPTION
OVERVIEW
 What is Exception?
 Type of Exception
 Exception Constructors
 General Format
 Exception handle
 Creating Custom Exception class
 Application 1
 Application 2
 Reference
TYPE OF EXCEPTION
 Exceptions can be:
 generated by the Java run-time system
Fundamental errors that violate the rules of
the Java language or the constraints of the
Java execution environment .
 manually generated by programmer’s code Such
exceptions are typically used to report some
error conditions to the caller of a method.
…TYPE OF EXCEPTION
 generated by the Java:
:Two sets of build-in exception classes
 unchecked exceptions – the compiler does
not check if a method handles or throws
there exceptions
 checked exceptions – must be included in the
method’s throws clause if the method
generates but does not handle them
TYPE OF EXCEPTION…
 Unchecked Built-In Exception: Methods that
generate but do not handle those exceptions
need not Declare them in the throws clause:
 Arithmetic Exception: arithmetic error such as divide-
by zero.
 ArrayIndexOutOfBoundsException:array index out
of bounds.
 IllegalArgumentException:illegal argument used to
invoke a method
TYPE OF EXCEPTION…
 Checked Built-In Exception:Methods that
generate but do not handle those
exceptions must declare them in the
throws clause:
 ClassNotFoundException :class not found
 CloneNotSupportedException: attempt to
clone an object that does not implement the
Cloneable interface.
 IllegalAccessException :access to a class is
denied.
OVERVIEW
 What is Exception?
 Type of Exception
 Exception Constructors
 General Format
 Exception handle
 Creating Custom Exception class
 Application 1
 Application 2
 Reference
EXCEPTION CONSTRUCTS
 Five constructs are used in exception handling:
 try – a block surrounding program statements to
monitor for exceptions.
 catch – together with try, catches specific kinds of
exceptions and handles them in some way
 finally – specifies any code that absolutely must be
executed whether or not an exception occurs
 throw – used to throw a specific exception from
the program.
 throws – specifies which exceptions a given
method can throw.
OVERVIEW
 What is Exception?
 Type of Exception
 Exception Constructors
 General Format
 Exception handle
 Creating Custom Exception class
 Application 1
 Application 2
 Reference
GENERAL FORMAT
try {
statement1;
statement2;
statement3;
} clause using a super-
catch(Exception1 ex) { class will catch all
sub-class exceptions
handling ex;
}
catch(Exception2 ex) {
handling ex; Therefore, specific
throw ex; exceptions should
appear before
} .more general ones
finally { In particular,
final Statements;(always executes) exception sub-
classes must
}
appear before
.super-classes
OVERVIEW
 What is Exception?
 Type of Exception
 Exception Constructors
 General Format
 Exception handle
 Creating Custom Exception class
 Application 1
 Application 2
 Reference
EXCEPTION HANDLING
 An exception is an object that describes an
exceptional condition (error) that has occurred
when executing a program.
 Exception handling involves the following:
 when an error occurs, an object (exception)
representing this error is created and thrown in the
method that caused it.
 that method may choose to handle the exception
itself or pass it on.
 either way, at some point, the exception is caught
and processed.
…EXCEPTION HANDLING
OVERVIEW
 What is Exception?
 Type of Exception
 Exception Constructors
 General Format
 Exception handle
 Creating Custom Exception class
 Application 1
 Application 2
 Reference
CREATING CUSTOM EXCEPTION
CLASS
 Use the exception classes in the API
whenever possible.
 Create custom exception classes if the
predefined classes are not sufficient.
 Declare custom exception classes by
extending Exception or a subclass of
Exception.
CREATING CUSTOM EXCEPTION
CLASS…
 When should you use the try-catch block in the code?
You should use it to deal with unexpected error
conditions. Do not use it to deal with simple,
expected situations. For example, the following code
try {
System.out.println(refVar.toString());
}
catch (NullPointerException ex) {
System.out.println("refVar is null");
}
CREATING CUSTOM EXCEPTION
CLASS…
 is better to be replaced by :
if (refVar != null)
System.out.println(refVar.toString());
else
System.out.println("refVar is null");
OVERVIEW
 What is Exception?
 Type of Exception
 Exception Constructors
 General Format
 Exception handle
 Creating Custom Exception class
 Application 1
 Application 2
 Reference
APPLICATION 1
import java.lang.Exception.*;
class MyException extends Exception {
private int detail;
MyException(int a) {
detail = a;
}
public String toString() {
return "MyException[" + detail + "]";
}
}
APPLICATION 1…
class ExceptionDemo {
/*The static compute method throws the
MyException exception whenever its a
argument is greater than 10:*/
static void compute(int a) throws MyException
{
System.out.println("Called compute(" + a +")");
if (a > 10) throw new MyException(a);
System.out.println("Normal exit");
}
APPLICATION 1…
public static void main(String args[]) {
try {
compute(1);
compute(20);
} catch (MyException e) {
System.out.println("Caught " + e);
}
Finally{System.out.println(“Executed”);}
}
}
OVERVIEW
 What is Exception?
 Type of Exception
 Exception Constructors
 General Format
 Exception handle
 Creating Custom Exception class
 Application 1
 Application 2
 Reference
APPLICATION 2
import java.util.InputMismatchException;
import java.util.Scanner;

public class DivideByZeroWithExceptionHandling


{
// demonstrates throwing an exception when a divide-by-zero occurs
public static int div( int x, int y )
throws ArithmeticException
{
return x / y; // possible division by zero
} // end method quotient

public static void main( String args[] )


{
Scanner scanner = new Scanner( System.in ); // scanner for input
boolean flag = true; // determines if more input is needed
…APPLICATION 2
do
{
try // read two numbers and calculate quotient
{
System.out.print( "Please enter an integer a: " );
int a = scanner.nextInt();
System.out.print( "Please enter an integer b: " );
int b = scanner.nextInt();

int result = div(a,b);


System.out.printf( "\nResult: %d / %d = %d\n",a,b,
result );
flag = false; // input successful; end looping
} // end try
…APPLICATION 2
catch ( InputMismatchException inputMismatchException )
{
System.err.printf( "\nException: %s\n",inputMismatchException );
scanner.nextLine(); // discard input so user can try again
System.out.println("You must enter integers. Please try again.\n" );
} // end catch
catch ( ArithmeticException arithmeticException )
{
System.err.printf( "\nException: %s\n", arithmeticException );
System.out.println("Zero is an invalid b. Please try again.\n" );
} // end catch
} while ( flag ); // end do...while
} // end main
} // end class DivideByZeroWithExceptionHandling
OVERVIEW
 What is Exception?
 Type of Exception
 Exception Constructors
 General Format
 Exception handle
 Creating Custom Exception class
 Application 1
 Application 2
 Reference
REFERENCE
 Java™ How to Program-Sixth Edition
 Java Workshop
 Thinking in java
 Lectures of Dr.hani shararh master1/2009-
2010
?? Any Question
THANK YOU

You might also like