Upload
meljun-cortes-mbampa
View
227
Download
0
Embed Size (px)
Citation preview
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 1/50
Introduction to Programming 2 1
2 Exceptions and Assertions
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 2/50
Introduction to Programming 2 2
Topics
● What are Exceptions?
– Introduction
– The Error and Exception Classes
– An Example
● Catching Exceptions
– The try -catch Statements
– The finally Keyword
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 3/50
Introduction to Programming 2 3
Topics
● Throwing Exceptions
– The throw Keyword
– The throws Keyword
● Exception Categories
– Exception Classes and Hierarchy
– Checked and Unchecked Exceptions
– User-Defined Exceptions
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 4/50
Introduction to Programming 2 4
Topics
● Assertions
– What are Assertions?
– Enabling and Disabling Assertions
– Assert Syntax
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 5/50
Introduction to Programming 2 5
What are Exceptions?
● Definition:
– Exceptional events
– Errors that occur during runtime
– Cause normal program flow to be disrupted – Examples
● Divide by zero errors
● Accessing the elements of an array beyond its range
● Invalid input
● Hard disk crash
● Opening a non-existent file
● Heap memory exhausted
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 6/50
Introduction to Programming 2 6
The Error and Exception
Classes● Throwable class
– Root class of exception classes
– Immediate subclasses
●
Error ● Exception
● Exception class
– Conditions that user programs can reasonably deal with
– Usually the result of some flaws in the user program code
– Examples
● Division by zero error
● Array out-of-bounds error
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 7/50
Introduction to Programming 2 7
The Error and Exception
Classes● Error class
– Used by the Java run-time system to handle errors occurring in therun-time environment
– Generally beyond the control of user programs
– Examples
● Out of memory errors
● Hard disk crash
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 8/50
Introduction to Programming 2 8
Exception Example
1 class DivByZero {
2 public static void main(String args[]) {
3 System.out.println(3/0);
4 System.out.println(“Pls. print me.”);5 }
6 }
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 9/50
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 10/50
Introduction to Programming 2 10
Catching Exceptions:
The try -catch Statements● Syntax:
try {
<code to be monitored for exceptions>
} catch (<ExceptionType1> <ObjName>) {
<handler if ExceptionType1 occurs>
}
...
} catch (<ExceptionTypeN> <ObjName>) {<handler if ExceptionTypeN occurs>
}
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 11/50
Introduction to Programming 2 11
Catching Exceptions:
The try -catch Statements1 class DivByZero {
2 public static void main(String args[]) {
3 try {
4 System.out.println(3/0);5 System.out.println(“Please print me.”);
6 } catch (ArithmeticException exc) {
7 //Division by zero is an ArithmeticException
8 System.out.println(exc);
9 }
10 System.out.println(“After exception.”);
11 }
12 }
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 12/50
Introduction to Programming 2 12
Catching Exceptions:
The try -catch StatementsOutput:
java.lang.ArithmeticException: / by zero
After exception.
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 13/50
Introduction to Programming 2 13
Catching Exceptions:The try -catch Statements
●
Multiple catch example1 class MultipleCatch {
2 public static void main(String args[]) {
3 try {
4 int den = Integer.parseInt(args[0]);
5 System.out.println(3/den);
6 } catch (ArithmeticException exc) {
7 System.out.println(“Divisor was 0.”);
8 } catch (ArrayIndexOutOfBoundsException exc2) {
9 System.out.println(“Missing argument.”);
10 }
11 System.out.println(“After exception.”);
12 }
13 }
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 14/50
Introduction to Programming 2 14
Catching Exceptions:The try -catch Statements
No argument was passed:Missing argument.
After exception.
Passing 1 as argument:
3
After exception.
Passing 0 as argument:
Divisor was 0.
After exception.
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 15/50
Introduction to Programming 2 15
Catching Exceptions:
The try -catch Statements● Nested try s
1 class NestedTryDemo {
2 public static void main(String args[]){
3 try {
4 int a = Integer.parseInt(args[0]);
5 try {
6 int b = Integer.parseInt(args[1]);
7 System.out.println(a/b);8 } catch (ArithmeticException e) {
9 System.out.println(“Div by zero error!");
10 }
11 //continued...
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 16/50
Introduction to Programming 2 16
Catching Exceptions:
The try -catch Statements11 } catch (ArrayIndexOutOfBoundsException exc) {
12 System.out.println(“Need 2 parameters!");
13 }
14 }
15 }
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 17/50
Introduction to Programming 2 17
Catching Exceptions:
The try -catch Statements No argument:
Need 2 parameters!
15:Need 2 parameters!
15 3:
5
15 0:
Div by zero error!
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 18/50
Introduction to Programming 2 18
Catching Exceptions:
The try -catch Statements● Nested try s with methods
1 class NestedTryDemo2 {
2 static void nestedTry(String args[]) {
3 try {4 int a = Integer.parseInt(args[0]);
5 int b = Integer.parseInt(args[1]);
6 System.out.println(a/b);
7
} catch (ArithmeticException e) {8 System.out.println("Div by zero error!");
9 }
10 }
11 //continued...
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 19/50
Introduction to Programming 2 19
Catching Exceptions:
The try -catch Statements12 public static void main(String args[]){
13 try {
14 nestedTry(args);
15 } catch (ArrayIndexOutOfBoundsException e) {
16 System.out.println("Need 2 parameters!");
17 }
18 }
19 }
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 20/50
Introduction to Programming 2 20
Catching Exceptions:
The try -catch Statements No argument:
Need 2 parameters!
15:Need 2 parameters!
15 3:
5
15 0:
Div by zero error!
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 21/50
Introduction to Programming 2 21
Catching Exceptions:
The finally Keyword● Syntax:
try {
<code to be monitored for exceptions>
} catch (<ExceptionType1> <ObjName>) {<handler if ExceptionType1 occurs>
} ...
} finally {
<code to be executed before the try block ends>}
● Contains the code for cleaning up after a try or a catch
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 22/50
Introduction to Programming 2 22
Catching Exceptions:
The finally Keyword● Block of code is always executed despite of differentscenarios:
– Forced exit occurs using a return, a continue or a break statement
– Normal completion
– Caught exception thrown
● Exception was thrown and caught in the method
– Uncaught exception thrown
● Exception thrown was not specified in any catch block in the method
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 23/50
Introduction to Programming 2 23
Catching Exceptions:The finally Keyword
1 class FinallyDemo {
2 static void myMethod(int n) throws Exception{
3 try {
4 switch(n) {
5 case 1: System.out.println("1st case");
6 return;
7 case 3: System.out.println("3rd case");
8 throw new RuntimeException("3!");
9 case 4: System.out.println("4th case");10 throw new Exception("4!");
11 case 2: System.out.println("2nd case");
12 }
13 //continued...
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 24/50
Introduction to Programming 2 24
Catching Exceptions:
The finally Keyword14 } catch (RuntimeException e) {
15 System.out.print("RuntimeException: ");
16 System.out.println(e.getMessage());
17 } finally {
18 System.out.println("try-block entered.");
19 }
20 }
21 //continued...
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 25/50
Introduction to Programming 2 25
Catching Exceptions:
The finally Keyword22 public static void main(String args[]){
23 for (int i=1; i<=4; i++) {
24 try {
25 FinallyDemo.myMethod(i);
26 } catch (Exception e){
27 System.out.print("Exception caught: ");
28 System.out.println(e.getMessage());
29 }
30 System.out.println();
31 }
32 }
33 }
Catching E ceptions
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 26/50
Introduction to Programming 2 26
Catching Exceptions:The finally Keyword
Output:1st case
try-block entered.
2nd case
try-block entered.
3rd case
RuntimeException: 3!
try-block entered.
4th case
try-block entered.
Exception caught: 4!
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 27/50
Introduction to Programming 2 27
Throwing Exceptions:
The throw Keyword● Java allows you to throw exceptions:
throw <exception object>;
● Example:throw new ArithmeticException(“testing...”);
Throwing Exceptions:
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 28/50
Introduction to Programming 2 28
Throwing Exceptions:The throw Keyword
1 class ThrowDemo {
2 public static void main(String args[]){
3 String input = “invalid input”;
4 try {
5 if (input.equals(“invalid input”)) {
6 throw new RuntimeException("throw demo");
7 } else {
8 System.out.println(input);
9 }
10 System.out.println("After throwing");11 } catch (RuntimeException e) {
12 System.out.println("Exception caught:" + e);
13 }
14 }
Throwing Exceptions:
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 29/50
Introduction to Programming 2 29
Throwing Exceptions:The throw Keyword
Output:
Exception caught:java.lang.RuntimeException:
throw demo
Throwing Exceptions:
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 30/50
Introduction to Programming 2 30
Throwing Exceptions:The throws Keyword
● A method is required to either catch or list all exceptions itmight throw
– Except for Error or RuntimeException, or their subclasses
● If a method may cause an exception to occur but does notcatch it, then it must say so using the throws keyword
– Applies to checked exceptions only
●
Syntax:<type> <methodName> (<parameterList>) throws
<exceptionList> {
<methodBody>
}
Throwing Exceptions:
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 31/50
Introduction to Programming 2 31
Throwing Exceptions:The throws Keyword
1 class ThrowingClass {
2 static void meth() throws ClassNotFoundException {
3 throw new ClassNotFoundException ("demo");
4 }
5 }
6 class ThrowsDemo {
7 public static void main(String args[]) {
8 try {
9 ThrowingClass.meth();
10 } catch (ClassNotFoundException e) {
11 System.out.println(e);
12 }
13 }
14 }
Throwing Exceptions:
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 32/50
Introduction to Programming 2 32
Throwing Exceptions:The throws Keyword
Output:
java.lang.ClassNotFoundException: demo
Exception Classes and
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 33/50
Introduction to Programming 2 33
Exception Classes andHierarchy
Exception Classes and
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 34/50
Introduction to Programming 234
Exception Classes andHierarchy
●
Multiple catches should be ordered from subclass tosuperclass.
1 class MultipleCatchError {
2 public static void main(String args[]){
3 try {
4 int a = Integer.parseInt(args [0]);
5 int b = Integer.parseInt(args [1]);
6 System.out.println(a/b);
7 } catch (Exception ex) {
8 } catch (ArrayIndexOutOfBoundsException e) {
9 }
10 }
11 }
Exception Classes and
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 35/50
Introduction to Programming 235
Exception Classes andHierarchy
Output:
MultipleCatchError.java:9: exception
java.lang.ArrayIndexOutOfBoundsException hasalready been caught
} catch
(ArrayIndexOutOfBoundsException e2) {
Checked and Unchecked
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 36/50
Introduction to Programming 236
Checked and UncheckedExceptions
● Checked exception
– Java compiler checks if the program either catches or lists theoccurring checked exception
– If not, compiler error will occur
● Unchecked exceptions
– Not subject to compile-time checking for exception handling
– Built-in unchecked exception classes
●
Error ● RuntimeException
● Their subclasses
– Handling all these exceptions may make the program cluttered andmay become a nuisance
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 37/50
Introduction to Programming 237
User-Defined Exceptions
● Creating your own exceptions
– Create a class that extends the RuntimeException or the Exception class
– Customize the class
● Members and constructors may be added to the class
● Example:
1 class HateStringExp extends RuntimeException {
2 /* No longer add any member or constructor */
3 }
U D fi d E ti
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 38/50
Introduction to Programming 238
User-Defined Exceptions● Using user-defined exceptions
1 class TestHateString {
2 public static void main(String args[]) {
3 String input = "invalid input";
4 try {
5 if (input.equals("invalid input")) {
6 throw new HateStringExp();
7 }
8 System.out.println("Accept string.");
9 } catch (HateStringExp e) {
10 System.out.println("Hate string!”);
11 }
12 }
13 }
U D fi d E ti
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 39/50
Introduction to Programming 2 39
User-Defined Exceptions
Output:
Hate string!
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 40/50
Introduction to Programming 2 40
What are Assertions?
● Allow the programmer to find out if an assumption was met
– Example: month
●
Extension of comments wherein the assert statement informsthe person reading the code that a particular condition shouldalways be satisfied
– Running the program informs you if assertions made are true or not
– If an assertion is not true, an AssertionError is thrown
● User has the option to turn it off or on at runtime
Enabling or Disabling
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 41/50
Introduction to Programming 2 41
Enabling or DisablingAssertions
● Program with assertions may not work properly if used byclients not aware that assertions were used in the code
● Compiling
– With assertion feature:
javac –source 1.4 MyProgram.java
– Without the assertion feature:
javac MyProgram.java
● Enabling assertions:
– Use the –enableassertions or –ea switch.
java –enableassertions MyProgram
Enabling or Disabling
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 42/50
Introduction to Programming 2 42
Enabling or DisablingAssertions: Using NetBeans
● Right click on project icon/name on Projects panel
● Select Properties from the drop-down menu
Enabling or Disabling
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 43/50
Introduction to Programming 2 43
Enabling or DisablingAssertions: Using NetBeans
●
Click on Run from Categories panel
Enabling or Disabling
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 44/50
Introduction to Programming 2 44
Enabling or DisablingAssertions: Using NetBeans
●
Enter -ea in the VM Option field & click on the OK button● Compile and run program as usual
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 45/50
Introduction to Programming 2 45
Assert Syntax
● Two forms:
– Simpler form:
assert <expression1>;
where
● <expression1> is the condition asserted to be true
– Other form:
assert <expression1> : <expression2>;
where
●
<expression1> is the condition asserted to be true● <expression2> is some information helpful in diagnosing why the statement failed
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 46/50
Introduction to Programming 2 46
Assert Syntax: Simpler Form
1 class AgeAssert {
2 public static void main(String args[]) {
3 int age = Integer.parseInt(args[0]);
4 assert(age>0);
5 /* if age is valid (i.e., age>0) */
6 if (age >= 18) {
7 System.out.println(“You're an adult! =)”);
8 }
9 }
10 }
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 47/50
Introduction to Programming 2 47
Assert Syntax: Simpler Form
args[0] is less than 1 (ex: 0)
Exception in thread "main" java.lang.AssertionError
at AgeAssert.main(AgeAssert.java:4)
Java Result: 1
args[0] ranges from 1 to 17 (ex: 5)
args[0] is at least equal to 18 (ex: 20)
Congrats! You're an adult! =)
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 48/50
Introduction to Programming 2 48
Assert Syntax: Second Form
1 class AgeAssert {
2 public static void main(String args[]) {
3 int age = Integer.parseInt(args[0]);
4 assert(age>0) : “age should at least be 1”;
5 /* if age is valid (i.e., age>0) */
6 if (age >= 18) {
7 System.out.println(“You're an adult! =)”);
8 }
9 }
10 }
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 49/50
Introduction to Programming 2 49
Assert Syntax: Second Form
args[0] is less than 1 (ex: 0)
Exception in thread "main" java.lang.AssertionError:
age should at least be 1
at AgeAssert.main(AgeAssert.java:4)
Java Result: 1
args[0] ranges from 1 to 17 (ex: 5)
args[0] is at least equal to 18 (ex: 20)
Congrats! You're an adult! =)
7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter02 Exceptions and Assertions
http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter02-exceptions-and-assertions 50/50
Introduction to Programming 2 50
Summary● Exceptions
– Definition
– try , catch and finally
– throw and throws
– Throwable, Exception, Error classes
– Checked and Unchecked Exceptions
– User-Defined Exceptions
● Assertions
– Definition
– Enabling and Disabling Assertions
– Assert Syntax