Found 4338 Articles for Java 8

Are the instances of Exception checked or unchecked exceptions in java?

Maruthi Krishna
Updated on 07-Aug-2019 09:16:03

493 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.There are two types of exceptions in Java.Unchecked Exception − An unchecked exception is the one which occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.Checked Exception − A checked exception is an exception that occurs at ... Read More

Why we can't initialize static final variable in try/catch block in java?

Maruthi Krishna
Updated on 07-Aug-2019 09:11:01

806 Views

In Java you can declare three types of variables namely, instance variables, static variables and, local variables.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.Class (static) variables − Class variables are variables declared within a class, outside any method, with the static keyword.Static methods in try-blockIn the same way, static variables belong to the class and can be accessed anywhere within the class, which contradicts with the definition of the local variable. Therefore, declaring ... Read More

How can we decide that custom exception should be checked or unchecked in java?

Maruthi Krishna
Updated on 03-Jul-2020 08:02:42

5K+ 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.User defined exceptionsYou can create your own exceptions in Java and they are known as user defined exceptions or custom exceptions.To create a user defined exception extend one of the above mentioned classes. To display the message override the toString() method or, call the superclass parameterized constructor by passing the message in String format.MyException(String msg){    super(msg); } Or, public String toString(){    return ... Read More

Can a method throw java.lang.Exception without declaring it in java?

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

160 Views

No, for that matter to throw any exception explicitly you need to create an object of that exception and throw it using the throw keyword.Without creating an object you cannot throw an exception explicitly, you might create a scenario that causes the respective exception.ExampleFollowing Java program throws a NullPointerExceptionpublic class ExceptionExample {    public static void main(String[] args) {       System.out.println("Hello");       NullPointerException nullPointer = new NullPointerException();       throw nullPointer;    } }OutputHello Exception in thread "main" java.lang.NullPointerException    at MyPackage.ExceptionExample.main(ExceptionExample.java:6)

How can I add condition in custom exceptions in java?

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

598 Views

While reading values from the user in the constructor or any method you can validate those values using if condition.ExampleIn the following Java example we are defining two custom exception classes verifying name and age.import java.util.Scanner; class NotProperAgeException extends Throwable{    NotProperAgeException(String msg){       super(msg);    } } class NotProperNameException extends Throwable{    NotProperNameException(String msg){       super(msg);    } } public class CustomException{    private String name;    private int age;    public static boolean containsAlphabet(String name) {       for (int i = 0; i < name.length(); i++) {          char ch = name.charAt(i);          if (!(ch >= 'a' && ch

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

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

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

990 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

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

Advertisements