0% found this document useful (0 votes)
38 views2 pages

Finally, Throw and Trhows

The document describes tasks involving exception handling in Java. Task 1 has the user create a class that divides by zero, catching the ArithmeticException, and using finally blocks. Task 2 has the user create their own exception class, throw it explicitly, and catch it. Task 2b has the user throw a predefined exception. Task 3 has the user create a method that throws an IOException using throws, and handle it in the main method using try-catch. The finally blocks and try-catch blocks demonstrate the normal exception handling flow.

Uploaded by

Adnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views2 pages

Finally, Throw and Trhows

The document describes tasks involving exception handling in Java. Task 1 has the user create a class that divides by zero, catching the ArithmeticException, and using finally blocks. Task 2 has the user create their own exception class, throw it explicitly, and catch it. Task 2b has the user throw a predefined exception. Task 3 has the user create a method that throws an IOException using throws, and handle it in the main method using try-catch. The finally blocks and try-catch blocks demonstrate the normal exception handling flow.

Uploaded by

Adnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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