Splash Screen-Thread and Try-Catch
Splash Screen-Thread and Try-Catch
mysplashscreen;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
}
}
Try-Catch in Java
Errors and Exceptions are conditions that may happen almost in every bit of your code. Being
aware of how to handle them plays an important part of how efficient your application will be.
Therefore, the goal of this post is to give you an idea of how it all works in Android Studio, and
by reading this you would be able to handle the majority of exceptions provided by java
programming language.
The most common method used in Android Studio is try-catch. As well as in java programming,
that consists of making the node "try" to include the routine that may fall into an error. Then, with
the node "catch" you tell the application what to do when catching a specific exception.
You can also handle specific exceptions by adding multiple "catch" nodes, and then even throw a new custom error. Note that "throw"
can also be used on the fly in other parts of your code.
try
{
// routine 1
// routine 2
// routine 3
}
catch (ExceptionRoutine1 e)
{
// That's good for diagnosing the problem
System.out.println("Thrown exception: " + e.getMessage());
}
catch (ExceptionRoutine2 e)
{
// custom exception
throw new Exception("Error in Routine 2", e);
}
catch (Exception e)
{
// General error can be anything*
// captured by the java class Exception
// print in the console detailed technical info
e.printStackTrace();
}
Notice that the "general catch" must always be at last in your code.
See how the exception classes are organized in Java.
Exception Hierarchy in Java
It is important to notice that some errors can be expected, for example when reading the user input and computing the data into a
variable. Several errors may come up which are predictable, such as:
However, some errors may appear from the logic or the structure of the code itself. Those can be harder to predict, but should be
always take into account while doing your code. No one wants the end user to get an error like:
Stack overflow
NullerPointException
Thread in Java
A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution
running concurrently.
Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or
may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority
initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.
When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of
some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:
The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an
exception that propagates beyond the run method.
There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should
override the run method of class Thread. An instance of the subclass can then be allocated and started. For example, a thread that
computes primes larger than a stated value could be written as above code: