Chapter 10
CS 200 - Programming I - Summer 2020
Mark Powers
Errors in code
● You cannot guarantee input values will be valid
● How to handle?
○ Return some special value
○ null
○ -1
Example: checkUsername method
// returns a code based on if everything is OK
public static int checkUsername(String username, ArrayList<String>
usernames){
if([Link](username)){
return -1; // -1 means username already exists
}
if([Link](‘ ‘) >= 0){
return -2; // -2 means username has an invalid character
}
return 0; // 0 means username is good to use
}
Example: checkUsername method
// returns- a
Wecode based on
must remember ifvalues
special everything is OK
- Requires a lot of checking in the code that calls this method
public static int checkUsername(String username, ArrayList<String>
- Hard to tell “is this code doing something” or “just error checking”
usernames){
if([Link](username)){
return -1; // -1 means username already exists
}
if([Link](‘ ‘) >= 0){
return -2; // -2 means username has an invalid character
}
return 0; // 0 means username is good to use
}
Exceptions:
● Built in Java constructs to handle errors
● try
○ Wraps code that might have an exception within it
● catch
○ Follows try, what to do when an exception occurs
● throw
○ Jumps to the right catch block, if one exists
Try/Catch
● try{
// code that may cause an exception
} catch(Exception ex) {
// what to do when exception occurs
}
Throw
● public static int checkUsername(String username,
ArrayList<String> usernames){
if([Link](username)){
return -1; // -1 means username already exists
}
if([Link](‘ ‘) >= 0){
return -2; // -2 means username has an invalid character
}
return 0; // 0 means username is good to use
}
Throw
● if([Link](username)){
throw new Exception(“Username already exists”);
}
if([Link](‘ ‘) >= 0){
throw new Exception(“Username cannot contain a space”);
}
[Link](username);
Throw and try/catch
● try{
if([Link](username)){
throw new Exception(“Username already exists”);
}
if([Link](‘ ‘) >= 0){
throw new Exception(“Username cannot contain a space”);
}
[Link](username);
}catch(Exception e){
[Link](“Exception!”);
[Link]([Link]());
}
Throws - Checked Exceptions
● If code throws an checked exception and it is not caught within that method it must
be declared - Otherwise there will be a compiler error
● public static void checkUsername(String username, ArrayList<String>
usernames) throws Exception{
if([Link](username)){
throw new Exception(“Username already exists”);
}
if([Link](‘ ‘) >= 0){
throw new Exception(“Username cannot contain a space”);
}
[Link](username);
}
Unchecked Exceptions - No need for throws
● No compiler error for not catching
● RuntimeException
● NullPointerException
● IndexOutOfBoundsException
● InputMismatchException
Checked Vs Unchecked
● Checked
○ You must either have catch or throws
○ Exception
○ Compiler error
● Unchecked
○ May occur while the program is running
○ No compiler error if missing catch
● Both can be caught using catch
Multiple catch
● We can specify which type of exception is caught
● First one that matches is run
● try{
// some code here
} catch (NullPointerException e) {
} catch (IndexOutOfBoundsException e) {
} catch (Exception e) { // catch all, any other exception
}
What is printed?
Scanner scnr = new Scanner(“hello”);
try{
int x = [Link]();
String s = null;
if([Link]()){
s = [Link]();
}
[Link]([Link](5));
} catch (NullPointerException e) {
[Link](“null”);
} catch (InputMismatchException e) {
[Link](“input mismatch”);
} catch (Exception e) { // catch all, any other exception
[Link](“other exception”);
}
What is printed?
Scanner scnr = new Scanner(“1”);
try{
int x = [Link]();
String s = null;
if([Link]()){
s = [Link]();
}
[Link]([Link](5));
} catch (NullPointerException e) {
[Link](“null”);
} catch (InputMismatchException e) {
[Link](“input mismatch”);
} catch (Exception e) { // catch all, any other exception
[Link](“other exception”);
}
What is printed?
Scanner scnr = new Scanner(“1 hi”);
try{
int x = [Link]();
String s = null;
if([Link]()){
s = [Link]();
}
[Link]([Link](5));
} catch (NullPointerException e) {
[Link](“null”);
} catch (InputMismatchException e) {
[Link](“input mismatch”);
} catch (Exception e) { // catch all, any other exception
[Link](“other exception”);
}
Stack trace
● When an exception occurs, the stack is stored
● Exception in thread "main" [Link]:
String index out of range: 3
at [Link]/[Link]([Link])
at [Link]/[Link]([Link])
at [Link]([Link])
Stack trace - What do we know?
Exception in thread "main" [Link]
at [Link]([Link])
at [Link]([Link])
at [Link]([Link])
Command Line Interface
● GUI - Graphical User Interface
○ Everything that uses graphics
○ Harder to develop
● CLI - Command Line Interface
○ User types in commands, interacts through text
○ Programs we have made in this class
○ Can be harder for a new user
○ May be easier for expert users
Command Line
● Windows - “Command Prompt”
● Mac/Linux - “Terminal”
Windows Mac/Linux
cd pwd Print current directory
cd <folder> cd <folder> Change directory
dir ls Show contents of directory
type cat Show file contents
help <command> man <command> Show help information
Running Java from command line
● First, change directory into location of [Link] file
● ls - should see [Link]
● type [Link] or cat [Link] - see contents of file
● javac [Link] - Compile java file
● ls - should see [Link]
● java MyClass - runs program
Command Line Arguments
● Text following the command, separated by whitespace
● ls eclipse-workspace/ Documents/
○ Arguments are “eclipse-workspace/” and “Documents/”
● When running java, the String[] args in main are the arguments after the
class name
○ java MyClass hello world
○ args = {“hello”, “world”}
● Arguments can be joined with “ “ on the command line
○ java MyClass “hello world” cs 200
○ args = {“hello world”, “cs”, “200”}
Usage:
● When a user forgets required arguments, print out a message
● E.g. “java” with no arguments
Eclipse
● Next to the run button, click arrow > run configurations
● Choose “arguments tab”, and enter under “program arguments”
Example:
● Write a program that takes a String as input, and outputs if it is a palindrome
● If one of the arguments is “debug”, then print out each pair of letters.
Exam 2 Questions