Exception in Java: by Montadher Issam Supervise Dr. Ali Kalakech
Exception in Java: by Montadher Issam Supervise Dr. Ali Kalakech
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;