CC102 Lesson 5 Bsit - PPT Java Packagesinput and Exception
CC102 Lesson 5 Bsit - PPT Java Packagesinput and Exception
Parsing and
Exception handling
LEARNING OUTCOMES:
At the end of the session, the students should be
able to:
• Design, implement, test and debug a program in
JAVA that uses each of the following fundamental
programming construct: basic computation, simple
I/O and basic error handling
• able to import specific packages
• apply different parsing techniques
• produce an error handling functionality on our
program
CC102 - FUNDAMENTALS OF PROGRAMMING
2
LESSON V – Java Packages, Parsing and Exception handling
Java Packages
• Packages are used in Java in order to prevent naming
conflicts, to control access, to make searching/locating and
usage of classes, interfaces, enumerations and annotations
easier, etc.
importing Packages
• These are the basic packages we need for basic input
using bufferreader class with exception handling.
import java.io.*
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
BufferReader, InputStreamReader
and Parsing
• In Java, there are multiple ways to obtain user input
from the keyboard. One of the objects that could
get the job done is a Buffered Reader using
the readline method, along with an Input Stream
Reader.
• the other method is using Scanner Class in Java.
Scanner is a class in java.util package used for
obtaining the input of the primitive types like int,
double etc.
• Parsing is converting the input from string to
desired datatype.
CC102 - FUNDAMENTALS OF PROGRAMMING 5
LESSON V – Java Packages, Parsing and Exception handling
BufferReader, InputStreamReader w/
import java.io.*; Parsing
public class InputStringtoInt{
public static void main(String[]args)throws IOException{
String StrNumericInput="";
int UserNumericInput=0;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please Enter a number: ");
StrNumericInput = in.readLine();
UserNumericInput= Integer.parseInt(StrNumericInput);
System.out.println(UserNumericInput+" is accepted");
}
}
CC102 - FUNDAMENTALS OF PROGRAMMING 8
LESSON V – Java Packages, Parsing and Exception handling
Exception handling
• The Exception Tree in Java
• Checked Exceptions and Runtime Exceptions
• Standard Exceptions
Exception Rules
• If an exception is not caught by any catch block:
1. If there is no finally block, the execution stops
right at the point of exception and returns to the
calling method
2. If there is a finally block, the execution jumps
from the point of exception to the finally block,
and returns to the calling method after the
finally block.
Exception Rules
• If the exception is caught by a catch block:
1. If there is a finally block, the execution jumps
after executing the catch block to the finally block,
and continues until the end of the method.
2. If there is no finally block, the execution jumps
after executing the catch block to the first
statement immediately after the last catch block
and continues to the end of the method.
import java.io.*;
public class InputStringtoInt{
Using the try and
public static void main(String[]args){ catch Blocks
String StrNumericInput="";
int UserNumericInput=0;
BufferedReader UserInput = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Please Enter a number: ");
StrNumericInput = UserInput.readLine();
UserNumericInput= Integer.parseInt(StrNumericInput);
System.out.println(UserNumericInput+" is accepted");
}
catch(Exception E){
System.out.println("Invalid Input");
}
}
}
CC102 - FUNDAMENTALS OF PROGRAMMING 19
LESSON V – Java Packages, Parsing and Exception handling
import java.io.*;
public class InputStringtoInt{
with finally Block
public static void main(String[]args){
String StrNumericInput="";
int UserNumericInput=0;
BufferedReader UserInput = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Please Enter a number: ");
StrNumericInput = UserInput.readLine();
UserNumericInput= Integer.parseInt(StrNumericInput);
}
catch(Exception E){
System.out.println("Invalid Input");
}
finally{
System.out.println(UserNumericInput+" is accepted");
}
}
}
CC102 - FUNDAMENTALS OF PROGRAMMING 20
LESSON V – Java Packages, Parsing and Exception handling
Using
Multiple
catch
Blocks
example