[go: up one dir, main page]

0% found this document useful (0 votes)
79 views7 pages

Splash Screen-Thread and Try-Catch

This document discusses try-catch blocks in Java for handling errors and exceptions. It explains that try-catch allows code to execute routines that may cause exceptions, with catch blocks to handle any exceptions. Specific exception types can be caught individually with multiple catch blocks. Finally, general exceptions are caught with a final catch-all catch block.

Uploaded by

Rana Arslan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views7 pages

Splash Screen-Thread and Try-Catch

This document discusses try-catch blocks in Java for handling errors and exceptions. It explains that try-catch allows code to execute routines that may cause exceptions, with catch blocks to handle any exceptions. Specific exception types can be caught individually with multiple catch blocks. Finally, general exceptions are caught with a final catch-all catch block.

Uploaded by

Rana Arslan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

package com.example.

mysplashscreen;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import static java.lang.Thread.sleep;

public class MainActivity extends AppCompatActivity {


TextView textView;
ImageView img1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.txtview);

Thread obj=new Thread(new Runnable() {


@Override
public void run() {
try {
sleep(6000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally {
Intent intent=new Intent(MainActivity.this,LoginScreen.class);
startActivity(intent);
finish();
}
}
});
obj.start();

}
}
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.

Simply like this:


try {
// routine 1
// routine 2
// routine 3
}
catch (Exception e) {
// Handle the error/exception
}
With this structure, the code will reach the node "catch" as soon as any exception has been caught. If it happens in the routine 2,
routine 3 will be then ignored and the code will execute the catch node. Then you can print the message of the exception, or print the
stack trace, or even define an alternative routine if necessary.

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.

public void ExampleMethod() throws IOException


{
if (conditionForError == true)
{
throw new IOException("Custom exception msg");
}

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:

 Array out of index exception


 Input is not in a valid format

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:

You might also like