Deck 12: Exception Handling
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
فتح الحزمة
قم بالتسجيل لفتح البطاقات في هذه المجموعة!
Unlock Deck
Unlock Deck
1/65
العب
ملء الشاشة (f)
Deck 12: Exception Handling
1
A(n) ____ statement is one that sends an Exception object out of a method so it can be handled elsewhere.
A) throw
B) catch
C) optional
D) assert
A) throw
B) catch
C) optional
D) assert
A
2
When a program contains multiple ____ blocks, they are examined in sequence until a match is found for the type of exception that occurred.
A) throw
B) finally
C) catch
D) try
A) throw
B) finally
C) catch
D) try
C
3
The keyword catch followed by an Exception type in the method header is used when a method throws an exception that it will not catch but that will be caught by a different method.
False
4
To use a method to its full potential, you must know the method name, return type, type and number of arguments required, and type and number of exceptions the method throws.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
5
Many developers believe that it is poor style for a method to throw more than ____ type(s) of exceptions.
A) one
B) two
C) three or four
D) eight or ten
A) one
B) two
C) three or four
D) eight or ten
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
6
A catch block is a method that can be called directly and takes an argument that is some type of exception.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
7
____ represents the degree to which a system is resilient to stress, maintaining correct functioning.
A) Endurance
B) Robustness
C) Fault-tolerant
D) Strength
A) Endurance
B) Robustness
C) Fault-tolerant
D) Strength
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
8
When you have actions you must perform at the end of a try…catch sequence, you can use a ____ block.
A) finally
B) catch
C) throw
D) try
A) finally
B) catch
C) throw
D) try
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
9
The code within a finally block cannot execute if the preceding try block identifies an exception.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
10
Placing data conversion attempts in a try block allows you to handle potential data conversion errors caused by careless user entry.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
11
The ____ class represents more serious errors from which your program usually cannot recover.
A) Error
B) Throwable
C) Exception
D) Menu
A) Error
B) Throwable
C) Exception
D) Menu
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
12
When an exception is a checked exception, client programmers are forced to deal with the possibility that an exception will be thrown.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
13
Unplanned exceptions that occur during a program's execution are also called execution exceptions.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
14
You can place as many statements as you need within a try block, and you can catch as many exceptions as you want.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
15
Since variables declared within a try or catch block are local to that block, the variable goes out of scope when the try or catch block ends.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
16
To create your own throwable Exception class, you must extend a subclass of Catchable.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
17
Some programmers refer to a catch block as a catch ____.
A) method
B) phrase
C) statement
D) clause
A) method
B) phrase
C) statement
D) clause
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
18
The parent class of Error is ____.
A) Object
B) Exception
C) RuntimeError
D) RuntimeException
A) Object
B) Exception
C) RuntimeError
D) RuntimeException
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
19
What is the first program statement in a catch block?
A) the keyword catch
B) an opening curly brace
C) the keyword Exception
D) the statements that take the action you want to use to handle the error condition
A) the keyword catch
B) an opening curly brace
C) the keyword Exception
D) the statements that take the action you want to use to handle the error condition
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
20
____ statements are program statements that can never execute under any circumstances.
A) Stagnant
B) Error
C) Unreachable
D) Exception
A) Stagnant
B) Error
C) Unreachable
D) Exception
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
21
If a method throws an exception that it will not catch but that will be caught by a different method, you must also use the keyword ____ followed by an Exception type in the method header.
A) finally
B) try
C) catch
D) throws
A) finally
B) try
C) catch
D) throws
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
22
A(n) ____ is a Java language feature that can help you detect logical errors that do not cause a program to terminate, but nevertheless produce incorrect results.
A) error checker
B) thread
C) assertion
D) throw statement
A) error checker
B) thread
C) assertion
D) throw statement
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
23
The memory location known as the ____________________ is where the computer stores the list of method locations to which the system must return.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
24
The ____________________ class comprises less serious errors that represent unusual conditions that arise while a program is running and from which the program can recover.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
25
Any ____________________ block might throw an Exception for which you did not provide a catch block.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
26
Which of the following is NOT a component of a try block?
A) a closing curly brace
B) the keyword try
C) executable statements
D) a throw statement
A) a closing curly brace
B) the keyword try
C) executable statements
D) a throw statement
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
27
Programs would be less clear if you had to account for ____ exceptions in every method declaration.
A) unthrown
B) thrown
C) runtime
D) checked
A) unthrown
B) thrown
C) runtime
D) checked
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
28
If you want to ensure that a user enters numeric data, you should use ____ techniques that provide the means for your program to recover from the mistake.
A) error-handling
B) looping
C) exception-handling
D) decision
A) error-handling
B) looping
C) exception-handling
D) decision
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
29
The Java compiler does not require that you catch or specify ____ exceptions.
A) checked
B) runtime
C) return
D) optional
A) checked
B) runtime
C) return
D) optional
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
30
What is the correct syntax of an assert statement?
A) assert AssertionError : optionalErrorMessage
B) assert booleanExpression : optionalErrorMessage
C) assert stringExpression : optionalErrorMessage
D) assert booleanExpression = optionalErrorMessage
A) assert AssertionError : optionalErrorMessage
B) assert booleanExpression : optionalErrorMessage
C) assert stringExpression : optionalErrorMessage
D) assert booleanExpression = optionalErrorMessage
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
31
When you catch an Exception object, you can call ____________________ to display a list of methods in the call stack so you can determine the location of the statement that caused the exception.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
32
____ exceptions are the type that programmers should anticipate and from which programs should be able to recover.
A) Unchecked
B) Runtime
C) Checked
D) Thrown
A) Unchecked
B) Runtime
C) Checked
D) Thrown
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
33
When a constructor throws an exception, no object is constructed; its intended reference variable value will be ____.
A) final
B) null
C) negative
D) correct
A) final
B) null
C) negative
D) correct
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
34
Assertions are meant to be helpful in the ____ stage of a program.
A) development
B) testing
C) production
D) modeling
A) development
B) testing
C) production
D) modeling
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
35
A(n) ____ clause is used in the method header so that applications that use your methods are notified of the potential for an exception.
A) throws
B) return
C) stack
D) exception
A) throws
B) return
C) stack
D) exception
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
36
In the case where a method might throw more than one exception type, you specify a list of potential exceptions in the method header by separating them with ____.
A) spaces
B) semicolons
C) commas
D) periods
A) spaces
B) semicolons
C) commas
D) periods
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
37
In order to use a variable both with a try or catch block and afterward, you must declare the variable before the ____ block begins.
A) catch
B) try
C) main
D) finally
A) catch
B) try
C) main
D) finally
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
38
Which method constructor constructs a new exception with the specified detail message and cause?
A) Exception()
B) Exception(String message)
C) Exception(String message, Throwable cause)
D) Exception(Throwable cause)
A) Exception()
B) Exception(String message)
C) Exception(String message, Throwable cause)
D) Exception(Throwable cause)
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
39
When you use a(n) ____ statement, you state a condition that should be true, and Java throws an AssertionError when it is not.
A) if
B) boolean
C) assert
D) exception
A) if
B) boolean
C) assert
D) exception
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
40
If you create an object using Java's BigDecimal class, and then perform a division that results in a non-terminating decimal division such as 1/3, but specify that an exact result is needed, a(n) ____________________ is thrown.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
41
Match each term with the correct statement below.
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
Should always be true if the program is working correctly
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
Should always be true if the program is working correctly
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
42
Match each term with the correct statement below.
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
Constructs a new exception with the specified detail message
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
Constructs a new exception with the specified detail message
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
43
Match each term with the correct statement below.
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
Object-oriented techniques to manage errors
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
Object-oriented techniques to manage errors
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
44
What is unreachable code and how might using multiple catch blocks cause this? Provide an example.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
45
What are the elements that make up a try block?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
46
Match each term with the correct statement below.
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
A block of code you attempt to execute while acknowledging that an exception might occur
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
A block of code you attempt to execute while acknowledging that an exception might occur
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
47
Match each term with the correct statement below.
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
A language feature designed to make it harder to write bad code
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
A language feature designed to make it harder to write bad code
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
48
Match each term with the correct statement below.
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
A process that is crucial to an organization
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
A process that is crucial to an organization
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
49
What advantages does object-oriented exception handling provide?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
50
How is an Error class different from an Exception class?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
51
When a program contains multiple catch blocks, how are they handled?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
52
Match each term with the correct statement below.
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
Constructs a new exception with the specified cause and a detail message of cause.toString()
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
Constructs a new exception with the specified cause and a detail message of cause.toString()
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
53
What is a finally block and how would a programmer use it?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
54
What things might a programmer do to cause a potential exception in a program?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
55
What are unchecked exceptions? Give an example.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
56
Match each term with the correct statement below.
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
Applications designed so that they continue to operate when some part of the system fails
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
Applications designed so that they continue to operate when some part of the system fails
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
57
What advantage to programmers does the technique of cycling through the methods in the stack offer? Why?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
58
Match each term with the correct statement below.
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
The parent class of Exception
a.Throwable
f.Exception(Throwable cause)
b.Exception(String message)
g.fault-tolerant
c.exception handling
h.assert statement
d.syntactic salt
i.try block
e.mission critical
The parent class of Exception
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
59
An alternative to hard coding error messages into your Exception classes is creating a catalog of possible messages to use. What are the advantages of doing so?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
60
What is an Exception class? Give an example.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
61
import java.util.Scanner;
public class AssertTest
{
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter a number between 0 and 10: " );
int number = input.nextInt();
assert ( number >= 0 && number <= 10 ) : "Invalid number: " + number;
System.out.printf( "You entered %d\n", number );
}
}
The above code demonstrates the functionality of the assert statement. Explain what happens when an entered number is valid and when an entered number is out of range.
public class AssertTest
{
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter a number between 0 and 10: " );
int number = input.nextInt();
assert ( number >= 0 && number <= 10 ) : "Invalid number: " + number;
System.out.printf( "You entered %d\n", number );
}
}
The above code demonstrates the functionality of the assert statement. Explain what happens when an entered number is valid and when an entered number is out of range.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
62
import java.util.*;
import java.util.Scanner;
public class AssertionExample
{
public static void main( String args[] )
{
Scanner scanner = new Scanner( System.in );
System.out.print( "Enter a number between 0 and 20: " );
int value = scanner.nextInt();
____
"Invalid number: " + value;
System.out.printf( "You have entered %d\n", value );
}
}
In the code above, when the user enters the number, the scanner.nextInt() method reads the number from the command line. In the blank line provided, create an assert statement that determines whether the entered number is within the valid range (between 0 and 20). If the user entered a number that is out of range, then the "Invalid number" error should occur.
import java.util.Scanner;
public class AssertionExample
{
public static void main( String args[] )
{
Scanner scanner = new Scanner( System.in );
System.out.print( "Enter a number between 0 and 20: " );
int value = scanner.nextInt();
____
"Invalid number: " + value;
System.out.printf( "You have entered %d\n", value );
}
}
In the code above, when the user enters the number, the scanner.nextInt() method reads the number from the command line. In the blank line provided, create an assert statement that determines whether the entered number is within the valid range (between 0 and 20). If the user entered a number that is out of range, then the "Invalid number" error should occur.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
63
public static void main(String args[])
{
int a, b;
try
{
a = 0;
b = 42 / a;
System.out.println("This will not be printed.");
}
catch (ArithmeticException e)
{
System.out.println("Division by zero.");
}
System.out.println("After catch statement.");
}
The program above includes a try block and a catch clause that processes the ArithmeticException generated by the division-by-zero error. Explain how the try and catch blocks operate, and what the output will be following program execution.
{
int a, b;
try
{
a = 0;
b = 42 / a;
System.out.println("This will not be printed.");
}
catch (ArithmeticException e)
{
System.out.println("Division by zero.");
}
System.out.println("After catch statement.");
}
The program above includes a try block and a catch clause that processes the ArithmeticException generated by the division-by-zero error. Explain how the try and catch blocks operate, and what the output will be following program execution.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
64
import java.util.*;
public class DivisionMistakeCaught3
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int numerator, denominator, result;
try
{
System.out.print("Enter numerator >> ");
numerator = input.nextInt();
System.out.print("Enter denominator >> ");
denominator = input.nextInt();
result = numerator / denominator;
System.out.println(numerator + " / " + denominator + " = " + result);
}
catch(ArithmeticException mistake)
{
System.out.println(mistake.getMessage());
}
catch(InputMismatchException mistake)
{
System.out.println("Wrong data type");
}
}
}
Using the above code, describe what will happen if a user enters two usable integers. What will happen if a user enters an invalid noninteger value? What will happen if the user enters 0 for the denominator?
public class DivisionMistakeCaught3
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int numerator, denominator, result;
try
{
System.out.print("Enter numerator >> ");
numerator = input.nextInt();
System.out.print("Enter denominator >> ");
denominator = input.nextInt();
result = numerator / denominator;
System.out.println(numerator + " / " + denominator + " = " + result);
}
catch(ArithmeticException mistake)
{
System.out.println(mistake.getMessage());
}
catch(InputMismatchException mistake)
{
System.out.println("Wrong data type");
}
}
}
Using the above code, describe what will happen if a user enters two usable integers. What will happen if a user enters an invalid noninteger value? What will happen if the user enters 0 for the denominator?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck
65
public class exceptions
{
public static void main(String Args[])
{
int[] array = new int[3];
try
{
for(int a=0;a<4;++a)
{
array[a] = a;
}
System.out.println(array);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Out of bounds");
}
}
}
In the above code, the line System.out.println(array); gets skipped when an exception occurs. Write a finally block that will execute, and will execute a System.out.println(array); if there is an exception.
{
public static void main(String Args[])
{
int[] array = new int[3];
try
{
for(int a=0;a<4;++a)
{
array[a] = a;
}
System.out.println(array);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Out of bounds");
}
}
}
In the above code, the line System.out.println(array); gets skipped when an exception occurs. Write a finally block that will execute, and will execute a System.out.println(array); if there is an exception.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 65 في هذه المجموعة.
فتح الحزمة
k this deck