Java Module 4 CH Pater 10
Java Module 4 CH Pater 10
java.lang.ArithmeticException: / by zero
at Exc0.main(Exc0.java:4)
try: A suspected code segment is kept inside the try block.
● If you run the above code, you will get the following error:
ThrowsDemo.java:8: error: unreported exception IllegalAccessException; must
be caught or declared to be thrown
throw new IllegalAccessException("demo");
^
1 error
class ThrowsDemo {
static void throwOne() throws IllegalAccessException {
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwOne();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
finally
●finally creates a block of code that will be executed
after a try/catch block has been completed and before
the code following the try/catch block.
try {
System.out.println(myNumbers[10]);
catch (Exception e) {
finally {
}
●Chained Exception helps to identify a situation in
which one exception causes another Exception in an
application. For instance, consider a method that
throws an ArithmeticException because of an attempt
to divide by zero but the actual cause of exception was
an I/O error which caused the divisor to be zero.
Lab Activity/Exercise 22
Develop a JAVA program to raise a custom exception
(also called a user-defined exception) for the
DivisionByZero scenario, using try, catch, throw, and
finally.