Thursday, 16 April 2020

SE - 52 - Primitive Vs non-Primitive data types

In Java data-types are divided into 2 sections : Primitive and non-Primitive data types

Primitive data-types : 
A primitive data type specifies the size and type of variable values, and it has no additional methods. It doesn't references a object or we need not to create a new object for primitive data types.
There are 8 primitive data types :

  • byte
  • short
  • int
  • long
  • double
  • float
  • char
  • boolean

Non-Primitive data types :
Non-primitive data types are also called reference types as they refer to a object. They are not predefined as Primitive ones. Eg : String , Array , Class, Interface , etc



Note :
String is a special type of non-primitive data type in which we can use without creating object too.


Difference :

1. Primitive types are predefined (already defined) in Java having fixed size. Non-primitive types are created by the programmer and is not defined by Java (except for String).

2. Non-primitive types can be has additional methods to perform certain operations, while primitive types does not has any methods.

3. A primitive type has always a value, while non-primitive types can be null.

4. A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.

5. The size of a primitive type depends on the data type, while non-primitive types have all the same size.

6. Primitive types does not refer to any object while non-primitive  types are refered to objects


SE - 51 - Throw Vs Throws Vs Throwable !

As we saw in last blogs that the Throwable is parent Class of all the Exceptions/Errors.

Now we need to see difference between Throw and Throws :


Throws :
It is used to declare an exception to inform and to ensure the programmer that the exception might hit and it needs a handling. Throws is only for checked exceptions

  1. public void name() throws exception_class_name{ 
  2. //code
  3. }  
It is followed by a class.
It is given in method signature
We can give multiple exception in method signature



Throw :
It is used to explicitly throw an exception :

  1. throw new IOException("sorry device error);  
It is followed by instance of Exception class.
It is given in method body
There can be only 1 throw statement at once

SE - 50 - try-catch-finally notes !


  1. Nested try catch is possible
  2. Finally block is always executed even if exception is not handled. If try blocks give exception which is not handled then finally will be executed and exception will be thrown.
  3. Finally block in java can be used to put "cleanup" code such as closing a file, closing connection etc. 
  4. If there is return statement in try block , then also finally will be executed
  5. If there is return statement in try and finally both the blocks then finally will override the return statement of try block. Only return statement of finally will be considered.
  6. With finally block we can skip catch block. Only try and finally will work.
  7. When Finally is not executed - If the JVM exits while the try or catch code is being executed, then the finally block may not execute. If the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues. The try block runs to the end, and no exception is thrown.
  8. All checked and unchecked exceptions can be handled by try-catch block. But we generally handle checked exception because most of unchecked exception should be handled by programmer itself like NullpointerException
  9. we can write multiple try blocks to handle exceptions or we can write multiple exceptions in single catch : 

catch(SQLException | IOException e)

SE - 49 - Difference between Errors and Exceptions in JAVA

Errors is some serious problem in code which should not be handled in try catch while Exception is something which can be handled by try-catch block.

Major Differences :

1. All Errors are Unchecked but not all exceptions are unchecked

2. Errors can not be recovered or handled by try-catch but exceptions can be handled.

3. Exceptions are related to application where as Errors are related to environment in which application is running.

4. Checked Exceptions are known to compiler but errors are not

Wednesday, 15 April 2020

SE - 48 - Checked Vs Unchecked Exceptions!

Checked Exceptions are also called as Compile Time Exceptions as these are checked by compiler during compilation. It just ensures to get the error handled by user during coding. Few of the major compile time exceptions are : 

IOException -  Its subclasses are  : FileNotFoundException, EOFException, UnsupportedEncodingException, SocketException, and SSLException

SQLException - Its subclasses are  : SQLClientInfoException , SQLDataException , SQLFeatureNotSupportedException ,SQLIntegrityConstraintViolationException
SQLInvalidAuthorizationSpecException ,SQLSyntaxErrorException , SQLTransactionRollbackException ,SQLTransientConnectionException

DataAccessException
ClassNotFoundException
InvocationTargetException


UnChecked Exceptions are also called as Run Time Exceptions as these are identified during runtime. All Errors are Unchecked also. Runtime exception is superclass of all the run-time exceptions :  Few of the major compile time exceptions are : 

  • ArithmeticException
  • ArrayStoreException
  • ClassCastException
  • ConcurrentModificationException
  • EnumConstantNotPresentException
  • IllegalArgumentException - IllegalThreadStateException,NumberFormatException
  • IllegalMonitorStateException
  • IllegalStateException
  • IndexOutOfBoundsException - ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException
  • NegativeArraySizeException
  • NullPointerException
  • SecurityException
  • TypeNotPresentException
  • UnsupportedOperationException

SE - 47 - All about Exceptions !

Throwable 

-> Throwable is parent class(not interface) of all the exceptions and Errors.

-> We can use Throwable in catch block to catch all the exceptions :
     catch(Throwable t)

->The mostly used methods of Throwable class are :

public String getMessage()  - It returns a detailed message string of the Throwable instance (which may be NULL).

public StackTraceElement[] getStackTrace()  - An array of StackTraceElement as given by printStackTrace

Sample Code : 
   try{  
       int i=10/0;  
   }catch(Exception e){  
       StackTraceElement[] trace = e.getStackTrace();  
       System.err.println(trace[0].toString());  
       System.out.println(trace[0].getClass());  
       System.out.println(trace[0].getMethodName());  
       System.out.println(trace[0].getFileName());  
       System.out.println(trace[0].getLineNumber());  

}}

public Throwable getCause()  - It is used to fetch the cause of the Throwable or null if cause can't be determined. This function fetches the cause that was supplied by one of the constructors or that was set after creation with the initCause(Throwable) method. All the PrintStackTrace methods invoke getCause() method to determine the cause of the Throwable.


public void printStackTrace()  - The printStackTrace() method of Java Throwble class is used to print the Throwable along with other details like classname and line number where the exception occurred.


As I mentioned earlier throwable has its 2 kids : Error and Exception

An Error is some problem in the program which can't be handled by try catch block. The execution has to be stopped in case of error. Some common error examples are : 


  • InternalError
  • OutOfMemoryError
  • StackOverflowError
  • UnknownError
  • NoClassDefFoundError
  • UnsatisfiedLinkError
  • AbstractMethodError
  • IllegalAccessError
  • InstantiationError
  • NoSuchFieldError
  • NoSuchMethodError



Exceptions are reasonable and logical errors which can be handled by using try catch block. We can continue the execution . The methods of Exception class are inherited by Throwable. Some common exceptions in java are : 


  • InterruptedException
  • IOException
  • FileNotFoundException
  • ConnectException
  • UnknownHostException
  • ClassNotFoundException
  • IllegalAccessException
  • InstantiationException
  • NoSuchFieldException
  • NoSuchMethodException
  • RuntimeException
  • ArithmeticException
  • ArrayStoreException
  • ClassCastException
  • ConcurrentModificationException
  • IllegalArgumentException
  • IllegalThreadStateException
  • NumberFormatException
  • IllegalStateException
  • IndexOutOfBoundsException
  • ArrayIndexOutOfBoundsException
  • StringIndexOutOfBoundsException
  • NegativeArraySizeException
  • NullPointerException




Tuesday, 10 March 2020

SE - 46 - isVisible() Vs isDisplayed() Vs isEnabled()

isVisible() - It is a legacy method of selenium RC. If an element is made invisible by setting the CSS "visibility" property to "hidden", or the "display" property to "none", either for the element itself or one if it's ancestors then this method will fail.

IisDisplayed - It is a method of selenium 2 and further versions. This method ignores the CSS style attribute and looks for the presence/absence of an element on webpage.

isEnabled() - It is used to check if any element is enabled or not