Found 4338 Articles for Java 8

How IllegalArgumentException automatically handled inside 'if' condition in java?

Maruthi Krishna
Updated on 02-Jul-2020 14:44:42

751 Views

Whenever you pass inappropriate arguments to a method or constructor, an IllegalArgumentException is thrown. It is a Runtime exception therefore there is no need to handle this at the time of compilation.ExampleThe valueOf() method of the java.sql.Date class accepts a String representing a date in JDBC escape format yyyy-[m]m-[d]d and converts it into a java.sql.Date object.import java.sql.Date; import java.util.Scanner; public class IllegalArgumentExample {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter your date of birth in JDBC escape format (yyyy-mm-dd) ");       String dateString = sc.next();   ... Read More

What is the difference between throw e and throw new Exception(e) in catch block in java?

Maruthi Krishna
Updated on 02-Jul-2020 14:46:36

6K+ Views

An exception is an issue (run time error) occurred during the execution of a program. Here are some example scenarios −If you have an array of size 10 if a line in your code tries to access the 11th element in this array.If you are trying to divide a number with 0 which (results to infinity and JVM doesn’t understand how to valuate it).When exception occurs the program terminates abruptly at the line that caused exception, leaving the remaining part of the program unexecuted. To prevent this, you need to handle exceptions.There are two types of exceptions in java.Unchecked Exception ... Read More

Handling IllegalComponentStateException in java.

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

294 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

How to handle MalformedURLException in java?

Maruthi Krishna
Updated on 02-Jul-2020 14:31:32

1K+ Views

While working with client-server programming in Java (JSE), if you are using java.net.URL class object in your program, you need to instantiate this class by passing a string representing required URL to which you need to establish connection. If the url you have passed in the string which cannot be parsed or, without legal protocol a MalformedURLException is generated.ExampleIn the following Java example we are tring to get establish a connection to a page and publishing the response.We have tampered the protocol part, changed it to htt, which should be http or, https.import java.util.Scanner; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; ... Read More

What is the cause of NoSuchElementException and how can we fix it in java?

Maruthi Krishna
Updated on 02-Jul-2020 14:33:06

1K+ Views

What is the cause of NoSuchElementException and how can we fix it in java?An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred 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.Cause for NosuchElementExceptionThis 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 ... Read More

IlleagalStateException Vs NoSuchElementException in java?

Maruthi Krishna
Updated on 19-Sep-2019 15:31:21

145 Views

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 it throws an java.lang.IllegalStateException.Example: ... Read More

Different scenarios that cause NoSuchElementException in Java.

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

234 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

What are try, catch, finally blocks in Java?

Maruthi Krishna
Updated on 02-Jul-2020 14:35:00

2K+ Views

An exception is an issue (run time error) occurred during the execution of a program. For understanding purpose let us look at it in a different manner.Generally, when you compile a program, if it gets compiled without a .class file will be created, this is the executable file in Java, and every time you execute this .class file it is supposed to run successfully executing each line in the program without any issues. But, in some exceptional cases, while executing the program, JVM encounters some ambiguous scenarios where it doesn’t know what to do.Here are some example scenarios −If you ... Read More

How to 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

How to append data to a file in Java?

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

603 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

Advertisements