Java Articles - Page 269 of 745

How to loop the program after an exception is thrown 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

How to print custom message instead of ErrorStackTrace in java?

Maruthi Krishna
Updated on 02-Jul-2020 15:03:02

4K+ Views

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.Printing the Exception messageYou can print the exception message in Java using one of the following methods which are inherited from Throwable class.printStackTrace() − This method prints the backtrace to the standard error stream.getMessage() − This method returns the detail message string of the current throwable object.toString() − This message prints the short description of the current throwable object.Exampleimport java.util.Scanner; public class PrintingExceptionMessage { ... Read More

When does a Java Array Throws a NullPointerException?

Maruthi Krishna
Updated on 02-Jul-2020 14:40:03

4K+ Views

In Java there is default value for every type, when you don’t initialize the instance variables of a class Java compiler initializes them on your be-half with these values. Null is the default value of the object type, you can also manually assign null to objects in a method.Object obj = null;But, you cannot use an object with null value or (a null value instead of an object) if you do so, a NullPointerException will be thrown.Examplepublic class Demo {    String name = "Krishna";    int age = 25;    public static void main(String args[]) {       ... Read More

Why can I throw null in Java and why does it upcast to a NullPointerException?

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

1K+ Views

In Java there is default value for every type, when you don’t initialize the instance variables of a class Java compiler initializes them on your be-half with these values. Null is the default value of the object type, you can also manually assign null to objects in a method.Object obj = null;But, you cannot use an object with null value or (a null value instead of an object) if you do so, a NullPointerException will be thrown.Examplepublic class Demo {    String name = "Krishna";    int age = 25;    public static void main(String args[]) {       ... 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

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

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

986 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

405 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

2K+ 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

Importance of the parseBoolean() method in Java?

raja
Updated on 01-Jul-2020 06:13:23

184 Views

The parseBoolean() method is an important method of a Boolean class. The parseBoolean() is a static method and can parse the String method argument into a Boolean object. The parseBoolean() method of Boolean class returns the boolean represented by the string argument.Syntaxpublic static boolean parseBoolean(String s)Exampleimport java.util.Scanner; public class ParseBooleanMethodTest {    public static void main(String[] args) {       System.out.print("Are you ready to play cricket(true/false)?");       Scanner scanner = new Scanner(System.in);       String str = scanner.nextLine();       scanner.close();       // Convert the user input into boolean       boolean answer ... Read More

Advertisements