












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
public class Test. { public static int DEFAULT_EGGS = 5; public static void main(String[] args). {. Scanner keyboard = new Scanner(System.in);.
Typology: Lecture notes
1 / 20
This page cannot be seen from the preview
Don't miss anything!













Scanner keyboard = new Scanner(System.in);System.out.println("Please enter the number of eggs:");int numberOfEggs;try{
numberOfEggs = keyboard.nextInt(); } catch(Exception e){
numberOfEggs = DEFAULT_EGGS;System.out.println("Bad input!!!\nSetting the default value, "
} System.out.println(numberOfEggs); } }
Test Class (Handling multiple exceptions) import java.util.Scanner;public class Test{ public static int DEFAULT_EGGS = 5;public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);System.out.println("Please enter the number of eggs:");int numberOfEggs = 0;int numberOfBasket = 0;int eggsPerBasket = 0;try{
numberOfEggs = keyboard.nextInt();System.out.println("Please enter the number of baskets:");numberOfBasket = keyboard.nextInt();eggsPerBasket = numberOfEggs/numberOfBasket; } catch(Exception e){
//Opps, now what to catch? Bad user input or division by zero? } System.out.println(numberOfEggs); } }
Handling multiple exceptions •^ Although it catches multiple exceptions, thispiece of code doesn’t execute all the statementsif an exception occurs at the first line of try block. •^ If there is a way to handle exception without atry-catch block, use it. (i.e. instead of using^ ArithmeticException
an if statement that checks if
would suffice)
-^ For each statement, form a different try-catchblock and catch every possible exception.
Accounting for Exceptions (
st^ option)
-^ Catching an exception inside of a method^ import java.util.InputMismatchException;import java.util.Scanner;public class Test{
public static void main(String[] args){
int numberOfEggs = getInput("Please enter the number of eggs:", 1);int numberOfBasket = getInput("Please enter the number of baskets:", 1);if (numberOfBasket < 1)
numberOfBasket = 1; int eggsPerBasket = numberOfEggs/numberOfBasket;System.out.println(numberOfEggs+"-"+numberOfBasket+"-"+eggsPerBasket); } public static int getInput(String prompt, int numOfTry){
Scanner keyboard = new Scanner(System.in);System.out.println(prompt);int k = 0;try{
k = keyboard.nextInt(); } catch(InputMismatchException e){
System.out.println("Bad input!!!(try number: "+numOfTry+")");k = getInput(prompt, numOfTry+1); } return k; } }
Accounting for Exceptions (
nd^
option)
-^ Catching an exception where the method is called^ public
static
void main(String[]
args)
int numberOfEggs
try{
numberOfEggs = getInput("Please
enter
the^ number
of^ eggs:");
}^ catch
(InputMismatchException
e){
numberOfEggs = 5; } int numberOfBasket =
try{
numberOfBasket =
getInput("Please
enter
the^ number
of^ baskets:");
}catch
(InputMismatchException
e){
numberOfEggs = -1;numberOfBasket =
… } public
static
int^ getInput(String
prompt)
throws
InputMismatchException{
Scanner
keyboard
= new Scanner(System.in);
System.out.println(prompt);int k =
keyboard.nextInt(); return
k;
}
Implementing New ExceptionClasses •^ Don’t forget catch handlers won’t be partof user-defined exception classes •^ In other words you should explicitlyindicate in your program when to throwyour own exception •^ Test class example continued:
throws
Clause Not Allowed in
actionPerformed •^ A throws clause cannot be added to methodactionPerformed in any action listener class. •^ Any exception thrown in methodactionPerformed must be caught in methodactionPerformed. •^ Similarly, if method windowClosing is redefinedin a window listener class, you may not add athrows clause to method windowClosing.