Finally, Throw and Trhows

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

TASK 1 :

Part (a) : Create a class Example that has a main method. Create an Arithmetic exception by
dividing an integer by zero and handle it using try and catch block. Use the finally block outside
the try catch block and display any output. Explain the Result.
Output:
First statement of try block
ArithmeticException
finally block
Out of try-catch-finally block
Part (b): Create a class MyFinallyBlock that has a main method. Throw any exception in first
try-catch block. Use a finally block after a first try-catch block. Create a second try-catch block
but it should not throw any exception. Use a second finally block outside the second try-catch
block and explain the output. The output should be like this:
Output:
Inside first catch block
Inside first finally block
Inside second finally block
TASK 2:
Part (a): How to throw your own exception explicitly using throw keyword
Create a class MyOwnException that extends an Exception class. Create another class
EmployeeTest that has a method employeeage. Use a throws keyword to throw your own
exception if age is less than zero. Handle this exception using try-catch block in main method.
The output should be like this:
Output:
MyOwnException: Age can't be less than zero
Part (b): How to throw an already defined exception using throw keyword
Create a class Exception2 that has two integer variables. Throw an Arithmetic exception using
throw keyword. The output should be like this:

Output:
Exception in thread main java.lang.ArithmeticException: First parameter is not valid
TASK 3:
Part(a): You Handle the exception
Create a class M that has a method named Method. Use the throws keyword to throw an
IOException. Create another class TestThrows2 that has a main method. Use the try-catch block
to handle this exception. The output should be like this:
Output:
Output:exception handled
normal flow...

You might also like