0% found this document useful (0 votes)
2 views7 pages

Week09 Exceptions

The document outlines a lab for CS 211 at George Mason University focused on Java exceptions and file I/O. It includes tasks such as creating methods to handle arithmetic exceptions, implementing proper exception handling in provided code, and creating a custom exception class. Students are instructed to submit their work by the end of the lab session.

Uploaded by

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

Week09 Exceptions

The document outlines a lab for CS 211 at George Mason University focused on Java exceptions and file I/O. It includes tasks such as creating methods to handle arithmetic exceptions, implementing proper exception handling in provided code, and creating a custom exception class. Students are instructed to submit their work by the end of the lab session.

Uploaded by

mainulfinances
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Week 09 Lab

Exceptions | File I/O Strikes back

CS 211 | Department of Computer Science | George Mason University


Outline

Java Exceptions
o On arithmetic or unsupported operations
o On file management operations
Throwables and making your own throwables
Notions

In Java, Exceptions and Errors are types of Throwables. A Throwable


provides a mechanism to trigger an unexpected event – throw:

throw new Throwable("Catch me if you can!");

Which can be listened to with several mechanisms – handling:


• Propagation:
public static void main(String[] args) throws Throwable {…}

• Catching: try{
// the Throwable may be thrown and propagated from a line within this block
}catch(Throwable t){
// and this block executes if the Throwable was in fact thrown.
}
Task 0

Download from Piazza: ExceptionBasics.java

Add a method that returns nothing, called iGotcha:


• “Try” to catch the arithmetic exception resulting of dividing a
number by zero.
• Once you caught it, print the line of code which caused the
situation based on the caught object.
• Call the method in your main method (commented in the code).
Task 1

• Download from Piazza: FileVader.java


• Follow the comments in the file to provide proper exception
handling to the existing code.
Task 2

Create the AnakinException class:


• It extends java.io.IOException
• It overrides public String getMessage():
Now it prints “I don’t like sand.”

Back in your FileVader class, mask* the IOException:


• Locate where your code is catching the IOException
• Now within that catch block, throw an AnakinException instead.
• Propagate the AnakinException beyond the main method.
*To mask an exception: to catch one exception and throw another
Submission
Do not forget!

• Submit your work by dragging your folders or package folders into


the Gradescope submission page (Week 08 Lab) by the end of the
lab time.
• Submit by the end of the lab time.

You might also like