Exception Handling in Java

What is an Exception?

--An exception is a problem that arises during the execution of a program
--Its an event that disrupts the normal flow of the program

How exception occurs?

Following are some scenarios where an exception occurs.

1. A user has entered an invalid data.

2. A file that needs to be opened cannot be found.

3. A network connection has been lost in the middle of communications or the JVM has run out of memory.


Exception Classes
Exception Class Hierarchy


Types of java exceptions:

1. Checked Exception -  Checked exceptions are checked at compile-time so this is also called compile time exceptions. Example of checked exceptions are : ClassNotFoundException, IOException, SQLException and so on.They occur usually interacting with outside resources/network resources e.g. database problems, network connection errors, missing files etc.

2. UnChecked Exception - Unchecked exceptions are not checked at compile-time, but they are checked at runtime. Also Called Run time Exceptions. Example of unchecked exceptions are : ArithmeticException, ArrayStoreException, ClassCastException and so on.

3. Error - Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.

How to handle exception?

Java exception handling is managed via five keywords:

try: this keyword is used to specify a block where we should place exception code. The try block must be followed by either catch or finally. It means, we can't use try block alone.

catch:  this block is used to handle the exception. It must be preceded by try block which means we can't use catch block alone. It can be followed by finally block later.

finally: this block is used to execute the important code of the program. It is executed whether an exception is handled or not.

throw: this keyword is used to throw an exception.

throws: this keyword is used to declare exceptions. It doesn't throw an exception. It specifies that there may occur an exception in the method. It is always used with method signature.

try catch syntax:

try{
//code that may throw an exception 
}catch(ExceptionClassName ref){
}


try finally block

try{
//code that may throw an exception 
}finally{
}

To get more details please watch below youtube video and Subscribe the channel.

https://www.youtube.com/watch?v=P-KR6Eq4CM0&feature=youtu.be

Part1:


Part2:


Different Elements on a Web Page


Textarea

The textarea element defines a multi-line input field.





TextBox

A text box is a rectangular area on the screen where you can enter text.
It is a common user interface element found in many types of software programs, such as web browsers, email clients, and word processors.

First name:

Last name:




Radio-Button

A radio button is an element of the graphical user interface (GUI) which allows a user to select a single item from a predefined list of options.
Radio buttons are often arranged in a group of at least two options.

Yes
No
Other

CheckBox

A check box, selection box, or tick box is a small interactive box that can toggled by the user to indicate an affirmative or negative choice. ...
Check boxes are used when more than one option may need to be checked or as an easy way to enable or disable a setting in a software program.

Checkbox1
Checkbox2
Checkbox3
Checkbox4

Input Type Button




Image

Selenium Automation

DropDown

A drop-down list (abbreviated drop-down; also known as a drop-down menu, drop menu, pull-down list, picklist) is a graphical control element,
similar to a list box, that allows the user to choose one value from a list. When a drop-down list is inactive, it displays a single value.







WebTable

Name Salary
James 5000
John 7000


Frames




Date-Picker







Auto-Complete




List-Box





Combo-Box



Double Click and Right Click




File Upload










Test Selenium Waits - Click on Button "Click Me" and it will Display message after 5 seconds



Hidden Field


BrokenLink


Same Element

first
second
third
forth
fifth
sixth

STUDENT REGISTRATION FORM

Student Registration Form
FIRST NAME (max 30 characters a-z and A-Z)
LAST NAME (max 30 characters a-z and A-Z)
DATE OF BIRTH
EMAIL ID
MOBILE NUMBER (10 digit number)
GENDER Male Female
ADDRESS
CITY (max 30 characters a-z and A-Z)
PIN CODE (6 digit number)
STATE (max 30 characters a-z and A-Z)
COUNTRY
HOBBIES
Drawing Singing Dancing Sketching
Others
QUALIFICATION




Sl.No. Examination Board Percentage Year of Passing
1 Class X
2 Class XII
3 Graduation
4 Masters
(10 char max) (upto 2 decimal)
COURSES APPLIED FOR BCA B.Com B.Sc B.A

super Keyword in Java

Three Important Usage of super keyword:

Use of super with variables: We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields.

Use of super with methods: It should be used if subclass contains the same method as parent class.

Use of super with constructors: super is used to invoke parent class constructor.

To get more details please watch below youtube video and Subscribe the channel.


Map Interface in Java

Properties: A map contains values on the basis of key, i.e. key and value pair. Each key and value pair is known as an entry. A Map contains unique keys.

A Map doesn't allow duplicate keys, but you can have duplicate values. HashMap and LinkedHashMap allow null keys and values, but TreeMap doesn't allow any null key or value.

A Map can't be traversed, so you need to convert it into Set using keySet() or entrySet() method.


HashMap Class

Properties: 
Java HashMap class contains values based on the key.
Java HashMap class contains only unique keys.
Java HashMap class may have one null key and multiple null values.
Java HashMap class is non synchronized.
Java HashMap class maintains no order.

Methods: void clear(), Object clone(), boolean containsKey(Object key), boolean containsValue(Object Value) , Object get(Object key), boolean isEmpty(), Set keySet(), Object put(Key k, Value v), int size(), Collection values(), Value remove(Object key)


LinkedHashMap Class

Properties: 
Java LinkedHashMap contains values based on the key.
Java LinkedHashMap contains unique elements.
Java LinkedHashMap may have one null key and multiple null values.
Java LinkedHashMap is non synchronized.
Java LinkedHashMap maintains insertion order.

Methods: void clear(), void size(), void isEmpty(), boolean containsKey(Object key), boolean containsValue(Object key), Object get(Object key)


TreeMap Class

Properties: 
Java TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.
Java TreeMap contains only unique elements.
Java TreeMap cannot have a null key but can have multiple null values.
Java TreeMap is non synchronized.
Java TreeMap maintains ascending order.

Methods: void clear(), void size(), void isEmpty(), boolean containsKey(Object key), boolean containsValue(Object key), Object get(Object key), Object firstKey(), Object lastKey()


HashTable Class:

Properties: Hashtable internally contains buckets in which it stores the key/value pairs. The Hashtable uses the key’s hashcode to determine to which bucket the key/value pair should map.
A Hashtable is an array of a list. Each list is known as a bucket. The position of the bucket is identified by calling the hashcode() method. A Hashtable contains values based on the key.
Java Hashtable class contains unique elements.
Java Hashtable class doesn't allow null key or value.
Java Hashtable class is synchronized.

Methods: void clear(), void size(), void isEmpty(), boolean containsKey(Object key), boolean containsValue(Object key), Object get(Object key)

To get more details please watch below youtube video and Subscribe the channel.




Java Data Types Questions and Answers

1. What are the primitive data types in Java ?
Answer: There are eight primitive data types.
byte.
short.
int.
long.
float.
double.
boolean.
char.

2. Is Java primitive data type stored on stack or heap?
Answer: Primitive types declared locally will be on the stack while primitive types that are defined as part of an object instance are stored on the heap. Local variables are stored on stack while instance and static variables are stored on the heap.

3. What is the output?
Answer: 
System.out.println(1.0/0);
There will be no exception instead it prints Infinity.
1.0 is a double literal and double datatype supports infinity.

4. What are the Wrapper classes available for primitive types ?
Answer:  following are the wrapper classes available:
boolean  - java.lang.Boolean
byte - java.lang.Byte
char - java.lang.Character
double - java.lang.Double
float - java.lang.Float
int - java.lang.Integer
long - java.lang.Long
short - java.lang.Short
void - java.lang.Void

5. What are wrapper classes ?
Answer:  They are wrappers to primitive data types. They allow us to access primitives as objects.

6. What is casting?
Answer:  There are two types of casting, casting between primitive numeric types and casting between object references. Casting between numeric types is used to convert larger values, such as double values, to smaller values, such as byte values. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference.

Java Variable Questions and Answers

1. What are different types of Varibales in Java?
Answer: Below are the different types of variables in Java.
Instance or non static variables,
Static or class variables,
Local or method variables,
parameters.

2. What are local variables in Java?
Answer:  A local variable is one that exists inside a method or a block of code and exists as long as that method or block is executing. Once the program reaches the end of the method (or block), the local variable disappears from memory. The next time the method is called, a completely new version of the local variable comes into existence. One of the most common types of local variables is a parameter. This variable need to be initialized before using it. The local or method level variables are not initialized to any default value, neither primitives nor object references.

3. Is local variable thread safe in java?
Answer:  Yes. All local variables defined in your program will be allocated memory in the stack. So, When you create a thread it will have its own stack created. Two threads will have two stacks and one thread never shares its stack (that is, local variables) with other thread.

4. What is inline value in Java?
Answer:  Inline value represents the literals in a Java program.
For example, String str = "Hello", here the string "Hello" is a inline value.

5. What are Instance variables?
Answer:  Instance variables are variables that are defined at the class level. Instance variables need not have to be initialized since they are automatically initialized to its default value during object creation.

6. How the object references are initialized when it is a instance variable?
Answer: Object references are initialized to null in case of instance variables.

7. Is Instance variables thread safe in Java?
Answer: No

8. What is the advantage of using arrays over variables ?
Answer:  Arrays provide a structure wherein multiple values can be accessed using single reference and index. This helps in iterating over the values using loops.

9. What is Constant?
Answer: The variable should be declared with static and final modifiers. static ensures that only one variable exists for all instances of the class and final makes its value not changeable once initialized.
static final int MATH_MARKS = 50;

10. What are static variables?
Answer: Static variables are class level variable where all instances of the class refer same variable. When an instance update its value, all the other objects will see the new value. They are loaded at run time when the respective Class is loaded.

11. Explain static keyword in Java.
Answer:  The static keyword can be applied to,
static variables,
static methods,
static block,
static inner class,
and interface static method (introduced in Java 8).

12. What is a static class in Java?
Answer:  A Class can be made static only if it is a nested class (class within a class). The nested static class can be accessed without having an object of outer class. It can access static data members of outer class including private. Static nested class cannot access non-static data member or method.

If you have the static member inside static nested class, it can be accessed directly and you don't have to create instance of static nested class.

13. What is a static block?
Answer:  A static block, is a block of code inside a Java class that will be executed when a class is first loaded into the JVM. Mostly the static block will be used for initializing the variables.
Static block will be called only one while loading and it cannot have any return type also not have
'this' or 'super' keyword. Java class can have more than one static block. It will be executed in the
same order as it appears.

14. Can we overload static methods in java?
Answer: Yes, we can overload static methods in java.

15. Can we override static methods in java?
Answer:  No, we can not override static methods in java.

16. Can we execute a program without main () method?
Answer:  Yes, it is possible in previous versions of Java, not since JDK 1.7 using static block.