Computer Maintenance
Computer Maintenance
Exception Handling
Objective
try • The "try" keyword is used to specify a block where we should place
exception code.
• The try block must be followed by either catch or finally. It means, we
can't use try block alone.
finally • The "finally" block is used to execute the important code of the
program.
• It is executed whether an exception is handled or not.
throw • The "throw" keyword is used to throw an exception.
try{
//put the code hear in try block
String str = null;
System.out.println(str.length());
} catch (NullPointerException e) {
e.getMessage();
}
Example #2
class Simple {
public static void main(String args[]) {
String[] greek = {"Alpha", "Beta"};
System.out.println(greek [2]);
}
}
• Exception Message
Example #3: Problem without exception handling
class Simple {
public void printString (String[ ] days) {
int data = 50 / 0;
System.out.println(“Rest of the code …”);
}
}
– ArithmeticException
– NullPointerException
– NumberFormatException
• Suppose I have a string variable that has characters, converting
this variable into digit will occur NumberFormatException.
– ArrayIndexOutOfBoundsException
• Basic format of Exception Handling is using try/catch.
• if an exception occurs in the try block, the rest of the try block code will
not execute. try{
//Code which might throw an exception
} catch (Exceptionclass e) {
//Code to handle an exception
}
Example 4: to resolve the exception in a catch block
– 'e' is just a variable. ('e' stands for exception, but you can rename it
anything you like, however, the data type has to remain 'Exception')
The 'e' variable stores an exception-type object in this case.
Multiple catch block
▪ A try block can be followed by one or more catch blocks.
▪ Each catch block must contain a different exception handler.
▪ So, if you have to perform different tasks at the occurrence of different
exceptions, use multi-catch block.
▪ Note:
– At a time only one exception occurs and at a time only one catch
block is executed.
– All catch blocks must be ordered from most specific to most general,
i.e. catch for ArithmeticException must come before catch for
Exception. try{
//Code which might throw an exception
} catch (ArithmeticException e) {
//Code to handle an exception
} catch (Exception e) {
//Code to handle an exception
}
Finally block
• Java finally block is a block that is
used to execute important code such
as closing connection, stream etc.
• Java finally block is always executed
whether exception is handled or not.
• Java finally block follows try or catch
block.
• Note: If you don't handle exception,
before terminating the program, JVM
executes finally block(if any).
• Why use java finally
• Finally block in java can be used to
put "clean-up" code such as closing
a file, closing connection etc.
• Finally Block: Syntax
try{
//Code which might throw an exception
} catch (ArithmeticException e) {
//Code to handle an exception
} catch (Exception_1 e1) {
//Code to handle an exception
} catch (Exception_2 e1) {
//Code to handle an exception
} catch (Exception_3 e1) {
//Code to handle an exception
} finally (Exception e) {
//The finally block always executes
}
Note the following
throws keyword
throw throws
Java throw keyword is used to Java throws keyword is used to
explicitly throw an exception. declare an exception.
Throw is used within the method. Throws is used with the method
signature.
You cannot throw multiple You can declare multiple exceptions
exceptions. e.g.
public void method()throws
IOException,SQLException.