Exception Handling In
Java
By Priyanka zope
Content
1:Introduction.
2:Error and Error Handling.
3:Exception.
4:Type Of Exception.
5:Coding Exception.
6:Advantages
Error and Error Handling.
v
An Error is any unexpected result obtained from a program during execution.
v
Unhandled errors may manifest themselves as incorrect
results or behavior, or as abnormal program termination.
v
Errors should be handled by the programmer, to prevent
them from reaching the user.
Exception
Exception- a better error handling.
Exception – an error condition that can occur during the
course of a program
execution
Ø
In Java, exceptions are objects themselves
Exception handling is another form of control structure
(like ifs and switch statements)
Ø
When an error is encountered, the normal flow
of the program is stopped and the exception is
Handled.
Types Of Exception
I:Checked Exception
Checked at compile time.
II:Unchecked Exception
Checked at runtime.
Coding Exception
1: try…..catch block
2:finally
3: throw
Try…catch block
•
To process an exception when it occurs, the line that throws the exception is
executed within a try block.
• A try block is followed by one or more catch clauses, which
contain code to process an exception.
•
The catch statement is used for catching exceptions.
try
{
<try block>
}
catch ( <ExceptionClass> <name> )
{
<catch block>
}
catch ( <ExceptionClass> <name> )
{
<catch block>}
Finally clause
•
A try statement can have an optional clause designated by
the reserved word finally.
• If no exception is generated, the statements in the finally
clause are executed after the statements in the try block
complete.
try {
// statements that throw exceptions
} catch(<exception>) {
// do stuff
} finally {
– // code here runs whether or not catch runs
}
Throw
All methods use the throw statement to throw an exception.
The throw statement requires a single argument: a throwable object.
Throwable objects are instances of any subclass of the Throwable class. Here's
an example of a throw statement.
Problem
IllegalArgumentException: illegal
parameter value
Problem
public class BankAccount
{
public void withdraw(double amount)
{
if (amount > balance)
{
?????????
}
balance = balance - amount;
}
. . .}
Solution
public class BankAccount
{
public void withdraw(double amount)
{
if (amount > balance)
{
IllegalArgumentException exception
= new IllegalArgumentException("Amount
exceeds balance");
throw exception;
}
balance = balance - amount;
}
. . .
}
Advantages
1:Separating Error-Handling Code from "Regular" Code
2:Grouping and Differentiating Error Types
3:consistency
4:flexibility
5:simplicity

Exception handling in java

  • 1.
  • 2.
    Content 1:Introduction. 2:Error and ErrorHandling. 3:Exception. 4:Type Of Exception. 5:Coding Exception. 6:Advantages
  • 3.
    Error and ErrorHandling. v An Error is any unexpected result obtained from a program during execution. v Unhandled errors may manifest themselves as incorrect results or behavior, or as abnormal program termination. v Errors should be handled by the programmer, to prevent them from reaching the user.
  • 4.
    Exception Exception- a bettererror handling. Exception – an error condition that can occur during the course of a program execution Ø In Java, exceptions are objects themselves Exception handling is another form of control structure (like ifs and switch statements) Ø When an error is encountered, the normal flow of the program is stopped and the exception is Handled.
  • 5.
    Types Of Exception I:CheckedException Checked at compile time. II:Unchecked Exception Checked at runtime.
  • 6.
    Coding Exception 1: try…..catchblock 2:finally 3: throw
  • 7.
    Try…catch block • To processan exception when it occurs, the line that throws the exception is executed within a try block. • A try block is followed by one or more catch clauses, which contain code to process an exception. • The catch statement is used for catching exceptions. try { <try block> } catch ( <ExceptionClass> <name> ) { <catch block> } catch ( <ExceptionClass> <name> ) { <catch block>}
  • 8.
    Finally clause • A trystatement can have an optional clause designated by the reserved word finally. • If no exception is generated, the statements in the finally clause are executed after the statements in the try block complete. try { // statements that throw exceptions } catch(<exception>) { // do stuff } finally { – // code here runs whether or not catch runs }
  • 9.
    Throw All methods usethe throw statement to throw an exception. The throw statement requires a single argument: a throwable object. Throwable objects are instances of any subclass of the Throwable class. Here's an example of a throw statement.
  • 10.
    Problem IllegalArgumentException: illegal parameter value Problem publicclass BankAccount { public void withdraw(double amount) { if (amount > balance) { ????????? } balance = balance - amount; } . . .}
  • 11.
    Solution public class BankAccount { publicvoid withdraw(double amount) { if (amount > balance) { IllegalArgumentException exception = new IllegalArgumentException("Amount exceeds balance"); throw exception; } balance = balance - amount; } . . . }
  • 12.
    Advantages 1:Separating Error-Handling Codefrom "Regular" Code 2:Grouping and Differentiating Error Types 3:consistency 4:flexibility 5:simplicity