Chapter - 1
Chapter - 1
Chapter - 1
TECHNOLOGY
& BUSINESS
COLLEGE
Program:
Computer
Science
Year 4 CS
(Extension)
Java Programming
Instructor:
Addisu M. (Asst. Prof)
1
2
Ch e
04 Exception Handling
4
Java Environment
Java environment includes a large number of development tools &
hundreds of classes and Methods.
The development tools are part of the system known as Java
Optional
Package Statement
Optional
Interface Statements
type name
You may assign a value to it highScore = 98;
Examples of other types of variables:
String studentName;
boolean gameOver;
19
Statements
A statement is a command that causes something to happen.
All statements are separated by semicolons ;
Example: System.out.println(“Hello, World”);
One-Dimensional Arrays
list of items can be given one variable name using only one subscript
and such a variable is called a single-subscripted variable or one-
dimensional array
32
Arrays
Declaration of Arrays
Arrays in java can be declared in two ways:
float nextFloat() used to scan the next token of the input as a float.
double nextDouble() used to scan the next token of the input as a double.
byte nextByte() used to scan the next token of the input as a byte.
boolean nextBoolean() used to scan the next token of input into a boolean value.
44
How to get input from user
Java Scanner Class
allows the user to read the input of primitive types like int, double,
long, short, float, and byte from the console/keyboard
belongs to java.util package
Test
False
expression ?
True
if statement may be implemented in different forms depending on
the complexity of conditions to be tested:
55
Decision Making Statements
if Statement - Simple
An if statement consists of a Boolean expression followed by one
General formula:
conditional expression ? Expression1:expression2;
conditional expression is evaluated first. If the result is true,
expression1 is evaluated and is returned as the value of the
conditional expression. Otherwise, expression2 is evaluated and its
value is returned.
65
Looping Statements
The process of repeatedly executing a block of statements is
known as looping.
java.lang.Throwable class is
Unchecked Exception
• classes that inherit RuntimeException
• errors that programmer can directly prevent from occurring
• Usually occur because of programming errors, when code is not robust enough to prevent them
• E.g., ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException,
IllegalArgumentException, etc
• not checked at compile-time, but they are checked at runtime
Error
• irrecoverable
• E.g., OutOfMemoryError, VirtualMachineError, AssertionError etc.
90
Exceptions Methods
list of methods available in the Throwable class
Function Description
Returns a detailed message about the exception that has
Output:
96
Common Scenarios of Java Exceptions
1) ArithmeticException
If we divide any number by zero, there occurs an ArithmeticException
2) NullPointerException int a = 50/0;//ArithmeticException+
If we have a null value in any variable, performing any operation on the
String s="abc";
4) ArrayIndexOutOfBoundsException
int i=Integer.parseInt(s);//NumberFormatException
array exceeds to it's size, ArrayIndexOutOfBoundsException occurs
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
97
throw vs throws
throws - used to declare an exception, which means it
works similar to the try-catch block. On the other hand,
THANKS!
Questions, Ambiguities,
Doubts, … ???