DOC PREVIEW
TRINITY CSCI 1311 - Exceptions and Using Files
Pages 9

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Exceptions and Using Files4/7/20082Opening Discussion■Let's look at solutions to the interclass problem.3Problems at Runtime■Sometimes when you run programs things don't go as you expect. We saw this before with the username program.■In the case of files, lots of things can go wrong that are outside of our control. For example, the file might not be there or it might not have what we were expecting in it. Networked files can be lost because of network problems. A good program will deal with these things instead of just dying.■So how should a method tell you if something went wrong and it didn't work the way it was supposed to?4Exceptions■The way that error conditions are dealt with in Java is with exceptions.■We can have our own code produce exceptions or code that we call can produce exceptions.■We saw the latter case yesterday when we were trying to use files.■As the name implies, exceptions are to be used in exceptional situations. You only use them when something out of the ordinary occurs.■There are many types of exceptions in Java that are part of the API. Later we will see how we can create our own.5Throwing Exceptions■If you write a method and something goes wrong in it, you can have the method throw an exception.throw new ExceptionType(message);■Most of the time you want to use exception types that already exist. That's all we can do with what we know now.■For example, if we had a method that was supposed to read five ints from a file and there weren't five ints to read we could throw an IOException with an appropriate message.6Throws Clauses■Many exceptions in Java have to be dealt with in some way. IOException is an example. If we throw one of these we have to explicitly say that the method will throw it.■We do this by adding a throws clause after the argument list of the method.public returnType methodName(args) throws ExceptionType {}■A method should include a throws clause if it knows the exception can occur, but doesn't know how to fix the situation.7try/catch Blocks■When a method does know what to do to deal with a particular exception, the code that can cause the exception can be put in a try block.■This is what we did yesterday with the files though we didn't do much to handle the problems.■A try block includes code you want to try to do. It is followed by catch blocks that catch each of the exception types that can occur in the try block.try {➔Statements} catch(ExceptionType1 name1) {➔Statements} catch(ExceptionType2 name2) {➔Statements} ...8Code with Files■Let's play around a bit more today with files. We could finish off the program we started last class and then consider how we would put files into the bank program.9Minute Essay■Exceptions give your program the ability to recover from problems. How does this compare to what happened in Alice programs if things went wrong?■Note that there is no class on Friday. Do you want to have Mike here to meet and go over questions you might have?■Interclass Problem – Do problem


View Full Document

TRINITY CSCI 1311 - Exceptions and Using Files

Course: Csci 1311-
Pages: 9
Documents in this Course
Arrays

Arrays

12 pages

Arrays

Arrays

10 pages

Applets

Applets

5 pages

Arrays

Arrays

8 pages

Methods

Methods

12 pages

Drawing

Drawing

8 pages

Load more
Download Exceptions and Using Files
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Exceptions and Using Files and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Exceptions and Using Files 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?