Download presentation
Presentation is loading. Please wait.
Published byJulie Bryant Modified over 9 years ago
1
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se
2
Overview Problems Handling problems Input / output Streams
3
Problems What can go wrong? Boundaries Problems User inputs Memory limits Resource limits Impossible maths IO errors
4
Problems User inputs public class ParseException extends Exception public class DataFormatException extends Exception
5
Problems Memory limits public class ArrayIndexOutOfBoundsException public class NullPointerException public class OutOfMemoryError
6
Problems Resource limits public class FileSystemNotFoundException Public class RunTimeErrorException
7
Problems Impossible maths public class ArithmeticException
8
Problems IO errors public class FileNotFoundException public UnknownHostException
9
Quiz Name five problems that can occur in any java program How does Java handle the problems?
10
Handling problems Two types of problem handling classes Checked Anticipate and recover Try / catch block Method throws Unchecked No special handling Errors in the machine or run time errors *
11
Handling problems Object Throwable ErrorException RuntimeException unchecked checked
12
Handling problems Create your own exceptions Create a subclass of one of the Java defined classes public class EndOfTheWorldException extends Exception Add information on the error Parameter to the constructor Use the “throw” key word to throw your exception if(bError) { throw new EndOfTheWorldException(); } public void func() throws Exception
13
Handling problems Example try { … } catch(IOException e) { … } catch(Exception e) { … } finally { … }
14
Handling problems Try with resources BufferedReader br = new BufferedReader(new FileReader(path)); try { try (BufferedReader br = new BufferedReader(new FileReader(path))) { Throwable.getSuppressed
15
Quiz What does checked and uncheck mean in regards to java exceptions? How do you create you own exceptions?
16
Input / Output Communicate with the outside world Java allows many ways to get data in a send data out GUI (graphic inputs / outputs) Keyboard / screen Database input / output File input / output
17
Input / Output
18
File IO is an abstraction “File” is the data Place to read and write to is a “stream”
19
Input / Output Java IO package Standard streams System.in- keyboard- InputStream System.out- console- PrintStream System.err- console- PrintStream Redirect System.setIn (Out) (Err) *
20
Input / Output Java InputStream is abstract System.in is a specific implementation Read strings Read numbers Reads in a whole line Use Scanner to break up the string
21
Input / Output Scanner class Breaks up a sting into “tokens” Uses a delimiter (like space, for example) Use next functions to get data out Use has functions to check for more tokens
22
Input / Output Scanner sc = new Scanner(System.in); String strMsg; while(sc.hasNext()) { strMsg = sc.next(); … }
23
Input / Output File Save data Exists between program runs Text file Simple and readable with other programs
24
Input / Output The File class Represents information on the file Path Director File attributes (hidden, is it readable? Does it exists?) System independent
25
Quiz What is a stream in java IO? What is a file in java IO?
26
Input / Output Class Reader and Class Writer Java super classes Read and write text (char) Open a file Read or write to a file Close a file
27
Input / Output try { FileReader fInput = new FileReader(“inputfile.txt”); FileWriter fOutput = new FileWriter(“outputfile.txt); nChar = fInput.read(); fOutput.write(nChar); } catch( …) { … } finally { if(fInput != null) fInput.close(); if(fOutput != null) fOutput.close(); }
28
Input / Output Open a file File object FileDescriptor String (file name) FileWriter allow you to append text as well Close
29
Input / Output Read char as int Read an array of char Write a char as int Write an array of char Write a string flush *
30
Input / Output More advanced IO Connect writer to another class PrintWriter(new FileWriter(“file.txt”)); println methods for all basic java objects No exceptions - checkError()
31
Input / Output BufferedReader new BufferedREader(new FileReader(“file.txt”)) String readLine(); Scanner Scanner fIn = new Scanner(new File(“file.txt”)); Scanner fIn = new Scanner(new FileReader(“file.txt”));
32
Streams Character Input Character Output Byte Output Byte Input Byte Output Data streams Processing streams
33
Streams Data Streams character Unicode Reader FileReader int read methods Char arrays
34
Streams Data Streams Byte 8 bit data InputStream FileInputStream int read() Byte arrays
35
Streams Data Streams Character Writer FileWriter Byte OutputStream FileOutputStream int write void write
36
Streams Processing Streams Character BufferedReader BufferedWriter InputStreamReader LineNumberReader Byte ObjectInputStream (output) ZipInputStream (output)
37
Quiz What are the two types of steams in java? What are the basic data types used?
38
Questions?
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.