Loop Program After Exception in Java

Maruthi Krishna
Updated on 06-Aug-2019 11:33:47

3K+ Views

Read the inputs and perform the required calculations within a method. Keep the code that causes exception in try block and catch all the possible exceptions in catch block(s). In each catch block display the respective message and call the method again.ExampleIn the following example we have an array with 5 elements, we are accepting two integers from user representing positions in the array and performing division operation with them, if the integer entered representing the positions is more than 5 (length of the exception) an ArrayIndexOutOfBoundsException occurs and, if the position chosen for denominator is 4 which is 0 ... Read More

When Do IllegalStateException and IllegalArgumentException Get Thrown in Java

Maruthi Krishna
Updated on 06-Aug-2019 11:11:11

2K+ Views

IllegalStateException:This exception is thrown when you call a method at illegal or inappropriate time an IlleagalStateException is generated.For example, the remove() method of the ArrayList class removes the last element after calling the next() or previous methods.After removing the element at the current position you need to move to the next element to remove it i.e. per one call of the next() method you can invoke this remove() method only once.Since the initial position of the list (pointer) will be before the first element, you cannot invoke this method without calling the next method.If you invoke the remove() method otherwise ... Read More

Handling IllegalComponentStateException in Java

Maruthi Krishna
Updated on 06-Aug-2019 09:11:49

442 Views

It is the sub class of IllegalStateException, This indicates that AWT component is not in an appropriate state i.e. if you are working with components but, not using them properly leads to this exception. There are several scenarios where this exception occursExampleIn the following example we are trying to build a sample login form here after setting the visibility of the window to true, we are trying to set the location by platform true which is inappropriate.import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LoginDemo extends JFrame implements ActionListener {    JPanel panel;    JLabel user_label, password_label, message;    JTextField ... Read More

Different Scenarios that Cause NoSuchElementException in Java

Maruthi Krishna
Updated on 06-Aug-2019 08:47:46

497 Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurs the program gets terminated abruptly and, the code past the line that generated the exception never gets executed. Each exception is represented by its respective class.NosuchElement ExceptionThis is a Runtime exception i.e. it occurs at the execution time.While accessing the contents of a collection, array or other objects using the accessor methods of an Enumeration, Iterator or, tokenizer, such as next() or nextElement(), if you try to get the elements from an empty object, or if you try to get the ... Read More

Create an Immutable Set in Java

Maruthi Krishna
Updated on 06-Aug-2019 08:34:07

1K+ Views

Whenever you need to create an object which cannot be changed after initialization you can define an immutable object. There are no specific rules to create immutable objects, the idea is to restrict the access of the fields of a class after initialization.A Set is an interface in collection framework, which does not allow duplicate values.You need to keep following points in mind while creating an immutable set −We should not be able to add or delete elements from it.we should not be able to add null values to an immutable set.Once you create an immutable set you cannot add ... Read More

Append Data to a File in Java

Maruthi Krishna
Updated on 06-Aug-2019 08:31:13

821 Views

In most scenarios if you try to write contents to a file, using the classes of the java.io package, the file will be overwritten i.e. data existing in the file is erased and the new data is added to it.But, in certain scenarios like logging exceptions into a file (without using logger frame works) you need to append data (message) in the next line of the file.You can do this using the Files class of the java.nio package. This class provides a method named write() which acceptsAn object of the class Path, representing a file.A byte array holding the data ... Read More

Can the Main Method in Java Return a Value?

Maruthi Krishna
Updated on 06-Aug-2019 08:26:53

2K+ Views

The public static void main(String args[]) is the entry point of a Java program Whenever you execute a program the JVM searches for the main method and starts executing the contents of it. If such method is not found the program gets executed successfully, but when you execute the program it generates an error.As the matter of fact you should declare the main method with public static as modifiers, void return type and String arguments if you change anything, JVM doesn’t considers as the entry point method and prompts an error at run time.Therefore, you cannot change the return type ... Read More

Can We Have Integers as Elements of an Enum in Java

Maruthi Krishna
Updated on 06-Aug-2019 08:15:03

11K+ Views

Enumeration (enum) in Java is a datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year etc.You can define an enumeration using the keyword enum followed by the name of the enumeration as −enum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Just like arrays, the elements/constants in an enumeration are identified using numbers starting from 0 in the above example the days are identified using numbers as shown in the following illustration −Integers as elements of an enumNo, we can have ... Read More

What is a Variable Field Property in Java

Maruthi Krishna
Updated on 06-Aug-2019 08:10:05

1K+ Views

In programming to hold data members we use variables, Java you can declare three types of variables namely, Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.Class (static) variables − Class variables are variables declared within ... Read More

Include Current Date When Logging Exceptions to a File in Java

Maruthi Krishna
Updated on 06-Aug-2019 07:50:57

340 Views

There are several logging frame works available to log your data into files. You can also define your own method. In either cases to add the current time to your logged exception you can use the LocalDateTime class.It is an immutable class representing the date-time, it stores date-time as year-month-day-hour-minute-second. The now() method of this class returns the current date-time.Using this method concatenate the current date and time to your exception message and write to your required file.Exampleimport java.io.FileOutputStream; import java.io.IOException; import java.time.LocalDateTime; import java.util.Arrays; import java.util.Scanner; public class LoggingToFile {    private static void writeLogToFile(Exception e) throws IOException { ... Read More

Advertisements