Exceptions in JAVA

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

Exceptions in JAVA

Exception
• The normal behavior or normal flow of the program is interrupted- due to some
unexpected events.
• For example, we open a file for reading the data. When the Open file call is
executed, we find the file we are trying to open is missing. This results in the
interruption of the normal flow of the program.
Exception Handling In Java
• When an exception occurs in the program, the program execution is terminated.
• As this is an abrupt termination, the system generates a message and displays it.
• The message generated by the system may be cryptic like some codes or unreadable.
• In Java, we can handle the exception and provide meaningful messages to the user
about the issue.
Reasons For The Exception To Occur

• If its an exception related to input, then the reason may be that the
input data is incorrect or unreadable.
• If we get an exception for file I/O then it is quite possible that the files
we are dealing with do not exist.
• At some other time, there may be errors like network issues, printer
not available or functioning, etc.
• Thus an error in the application is more severe and the applications
would crash when they encounter an error. Exceptions on the other
hand occur in code and can be handled by the programmer by
providing corrective actions.
Built-in Exceptions
• Built-in exceptions are the exceptions that are available in Java libraries
• Checked Exceptions: Checked exceptions are called compile-time
exceptions because these exceptions are checked at compile-time by
the compiler.
• Unchecked Exceptions: The unchecked exceptions are just opposite to
the checked exceptions. The compiler will not check these exceptions
at compile time. In simple words, if a program throws an unchecked
exception, and even if we didn’t handle or declare it, the program
would not give a compilation error.
• Java program to demonstrate a basic exception example.
• Here we provide a string variable initialized to a null value. When we try
to print this variable, an exception is thrown as the String value cannot be
null.
• we try to open a non-existing file and read from it.
• In the output, as the exception handler is absent, we get compilation errors for
checked exceptions.
• Now let us provide a throws clause for this program
Lab exercise
8. Write a JAVA program which has Class called Account that creates account with
Rs500 minimum balance, a deposit() method to deposit amount, a withdraw()
method to withdraw amount and also throws LessBalanceException if an account
holder tries to withdraw money which makes the balance become less than Rs500.
• i. A Class called LessBalanceException which returns the statement that says
withdraw amount (Rs) is not valid.
• ii. A Class which creates 2 accounts, both account deposit money and one account
tries to withdraw more money which generates a LessBalanceException take
appropriate action for the same.
class LessBalanceException extends Exception
{
public LessBalanceException(double amount)
{
super("Withdraw amount (" + amount + " Rs) is not possible. ");
}
}
class Account { public void withdraw(double amount) throws
double balance; LessBalanceException
{
static final double MIN_BALANCE = 500; if (balance - amount < MIN_BALANCE)
public Account() { throw new LessBalanceException(amount);
}
{ balance = MIN_BALANCE;
balance -= amount;
} System.out.println("Withdrawn " + amount +
public void deposit(double amount) " Rs. New balance: " + balance + " Rs");
}
{ balance += amount; public double getBalance()
System.out.println("Deposited " + amount + " Rs. New { return balance;
balance: " + balance + " Rs"); } }
}
public class TestAccount
{
// Try to withdraw from account2
public static void main(String[] args)
try
{ Account account1 = new Account(); {
Account account2 = new Account(); account2.withdraw(200);
}
// Deposit money into both accounts catch (LessBalanceException e)
account1.deposit(1000); {
account2.deposit(700); System.out.println("LessBalanceException:
" + e.getMessage());
// Try to withdraw from account1
try }
}
{ account1.withdraw(1600);
}
}
catch (LessBalanceException e)
{ System.out.println("LessBalanceException: "
+ e.getMessage());
}
• https://www.geeksforgeeks.org/polymorphism-in-java/
• https://www.geeksforgeeks.org/super-keyword/

You might also like