Understanding Java Exceptions
By Yasir Hussain
Subject JAVA
Yasir Hussain
BCA , MCA/ NIT SRINAGAR (Gold Medalist)
What are Java Exceptions ?
• An exception (or exceptional event) is a problem that arises during the execution of a program.
When an Exception occurs the normal flow of the program is disrupted and the
program/Application terminates abnormally, which is not recommended, therefore, these
exceptions are to be handled.
• An exception can occur for many different reasons. Following are some scenarios where an
exception occurs.
• A user has entered an invalid data.
• A file that needs to be opened cannot be found.
• A network connection has been lost in the middle of communications or the JVM has run out of
memory.
Errors occur In Program?
Compile Time Error
Run time Error
• Checked exceptions − A checked exception is an exception that is checked (notified) by the
compiler at compilation-time, these are also called as compile time exceptions.These exceptions
cannot simply be ignored, the programmer should take care of (handle) these exceptions.
• Compile-Time Errors: Errors that occur when you violate the rules of writing syntax are known
as Compile-Time errors.This compiler error indicates something that must be fixed before
the code can be compiled.
• Unchecked exceptions − An unchecked exception is an exception that occurs at the
time of execution.These are also called as Runtime Exceptions.These include
programming bugs, such as logic errors or improper use of an API. Runtime
exceptions are ignored at the time of compilation.
• Runtime Error These are the errors which are not detected by the compiler and produce It
includes errors such as dividing a number by zero, finding square root of a negative
number etc.Wrong results.They prevent the code from complete execution.
• Example: Number/zero 2/0:
Run time Error
What is Exceptional handling in java ?
• The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be
maintained.
• Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException,
• public class JavaExceptionExample{
• public static void main(String args[]){
• try{
• //code that may raise exception
• int data=100/0;
• }
• catch(ArithmeticException e)
• {
• System.out.println(e);
• }
• //rest code of the program
• System.out.println("rest of the code...");
• }
• }
• class Yasir
• {
• public static void main(String args [ ])
• {
• try {
• int a = 30, b = 0;
• int c = a/b;
• System.out.println ("Result = " + c);
• }
• catch(ArithmeticException e) or (Exception e)
• System.out.println ("Can't divide a number by 0");
• }
• }
• }
Values Defined
Using Scanner Function ?
Output
How JVM handle an Exception?
• Default Exception Handling : Whenever inside a method, if an exception has occurred, the
method creates an Object known as Exception Object and hands it off to the run-time
system(JVM).The exception object contains name and description of the exception, and
current state of the program where exception has occurred. Creating the Exception Object
and handling it to the run-time system is called throwing an Exception.There might be the
list of the methods that had been called to get to the method where exception was
occurred.This ordered list of the methods is called Call Stack.Now the following procedure
will happen.
• The run-time system searches the call stack to find the method that contains block
of code that can handle the occurred exception.The block of the code is
called Exception handler.
• The run-time system starts searching from the method in which exception
occurred, proceeds through call stack in the reverse order in which methods were
called.
• If it finds appropriate handler then it passes the occurred exception to it.
Appropriate handler means the type of the exception object thrown matches the
type of the exception object it can handle.
• If run-time system searches all the methods on call stack and couldn’t have found
the appropriate handler then run-time system handover the Exception Object
to default exception handler , which is part of run-time system.This handler
prints the exception information in the following format and terminates
program abnormally.
diagram to understand the flow of the call stack
Advantage of exception handling
• Exception handling ensures that the flow of the program doesn’t break when
an exception occurs. For example, if a program has bunch of statements and an
exception occurs mid way after executing certain statements then the
statements after the exception will not execute and the program will terminate
abruptly.
By handling we make sure that all the statements execute and the flow of
program doesn’t break.
•
Try Catchin Java – Exception handling
Tryblock
The try block contains set of statements where an exception can 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.
Syntaxof tryblock
try{ //statements that may cause an exception }
While writing a program, if you think that certain statements in a program can throw a exception,
enclosed them in try block and handle that exception
Catch block
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.You can catch different exceptions in different catch blocks.
When an exception occurs in try block, the corresponding catch block that handles that particular exception
executes. For example if an arithmetic exception occurs in try block then the statements enclosed in catch
block for arithmetic exception executes.
Syntax of try catch in java
Try
{
//statements that may cause an exception }
catch (exception(type) e(object))
{ //error handling code }
Finally block
• 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 etc.
Syntax of Finally block
try {
//Statements that may cause an exception
}
catch {
//Handling exception
}
finally {
//Statements to be executed }

Java Exceptions and Exception Handling

  • 1.
    Understanding Java Exceptions ByYasir Hussain Subject JAVA Yasir Hussain BCA , MCA/ NIT SRINAGAR (Gold Medalist)
  • 2.
    What are JavaExceptions ? • An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled. • An exception can occur for many different reasons. Following are some scenarios where an exception occurs. • A user has entered an invalid data. • A file that needs to be opened cannot be found. • A network connection has been lost in the middle of communications or the JVM has run out of memory.
  • 3.
    Errors occur InProgram? Compile Time Error Run time Error • Checked exceptions − A checked exception is an exception that is checked (notified) by the compiler at compilation-time, these are also called as compile time exceptions.These exceptions cannot simply be ignored, the programmer should take care of (handle) these exceptions. • Compile-Time Errors: Errors that occur when you violate the rules of writing syntax are known as Compile-Time errors.This compiler error indicates something that must be fixed before the code can be compiled.
  • 4.
    • Unchecked exceptions− An unchecked exception is an exception that occurs at the time of execution.These are also called as Runtime Exceptions.These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation. • Runtime Error These are the errors which are not detected by the compiler and produce It includes errors such as dividing a number by zero, finding square root of a negative number etc.Wrong results.They prevent the code from complete execution. • Example: Number/zero 2/0: Run time Error
  • 5.
    What is Exceptionalhandling in java ? • The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained. • Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, • public class JavaExceptionExample{ • public static void main(String args[]){ • try{ • //code that may raise exception • int data=100/0; • } • catch(ArithmeticException e) • { • System.out.println(e); • } • //rest code of the program • System.out.println("rest of the code..."); • } • }
  • 6.
    • class Yasir •{ • public static void main(String args [ ]) • { • try { • int a = 30, b = 0; • int c = a/b; • System.out.println ("Result = " + c); • } • catch(ArithmeticException e) or (Exception e) • System.out.println ("Can't divide a number by 0"); • } • } • } Values Defined
  • 7.
  • 8.
  • 9.
    How JVM handlean Exception? • Default Exception Handling : Whenever inside a method, if an exception has occurred, the method creates an Object known as Exception Object and hands it off to the run-time system(JVM).The exception object contains name and description of the exception, and current state of the program where exception has occurred. Creating the Exception Object and handling it to the run-time system is called throwing an Exception.There might be the list of the methods that had been called to get to the method where exception was occurred.This ordered list of the methods is called Call Stack.Now the following procedure will happen.
  • 10.
    • The run-timesystem searches the call stack to find the method that contains block of code that can handle the occurred exception.The block of the code is called Exception handler. • The run-time system starts searching from the method in which exception occurred, proceeds through call stack in the reverse order in which methods were called. • If it finds appropriate handler then it passes the occurred exception to it. Appropriate handler means the type of the exception object thrown matches the type of the exception object it can handle. • If run-time system searches all the methods on call stack and couldn’t have found the appropriate handler then run-time system handover the Exception Object to default exception handler , which is part of run-time system.This handler prints the exception information in the following format and terminates program abnormally.
  • 11.
    diagram to understandthe flow of the call stack
  • 13.
    Advantage of exceptionhandling • Exception handling ensures that the flow of the program doesn’t break when an exception occurs. For example, if a program has bunch of statements and an exception occurs mid way after executing certain statements then the statements after the exception will not execute and the program will terminate abruptly. By handling we make sure that all the statements execute and the flow of program doesn’t break. •
  • 14.
    Try Catchin Java– Exception handling Tryblock The try block contains set of statements where an exception can 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. Syntaxof tryblock try{ //statements that may cause an exception } While writing a program, if you think that certain statements in a program can throw a exception, enclosed them in try block and handle that exception Catch block 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.You can catch different exceptions in different catch blocks. When an exception occurs in try block, the corresponding catch block that handles that particular exception executes. For example if an arithmetic exception occurs in try block then the statements enclosed in catch block for arithmetic exception executes. Syntax of try catch in java Try { //statements that may cause an exception } catch (exception(type) e(object)) { //error handling code }
  • 15.
    Finally block • Afinally 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 etc. Syntax of Finally block try { //Statements that may cause an exception } catch { //Handling exception } finally { //Statements to be executed }