- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4338 Articles for Java 8
![Maruthi Krishna](https://www.tutorialspoint.com/assets/profiles/119958/profile/60_3780337-1641548568.png)
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
![Maruthi Krishna](https://www.tutorialspoint.com/assets/profiles/119958/profile/60_3780337-1641548568.png)
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
![Maruthi Krishna](https://www.tutorialspoint.com/assets/profiles/119958/profile/60_3780337-1641548568.png)
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
![Maruthi Krishna](https://www.tutorialspoint.com/assets/profiles/119958/profile/60_3780337-1641548568.png)
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)
![Maruthi Krishna](https://www.tutorialspoint.com/assets/profiles/119958/profile/60_3780337-1641548568.png)
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
![Maruthi Krishna](https://www.tutorialspoint.com/assets/profiles/119958/profile/60_3780337-1641548568.png)
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
![Maruthi Krishna](https://www.tutorialspoint.com/assets/profiles/119958/profile/60_3780337-1641548568.png)
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
![Maruthi Krishna](https://www.tutorialspoint.com/assets/profiles/119958/profile/60_3780337-1641548568.png)
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
![Maruthi Krishna](https://www.tutorialspoint.com/assets/profiles/119958/profile/60_3780337-1641548568.png)
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
![Maruthi Krishna](https://www.tutorialspoint.com/assets/profiles/119958/profile/60_3780337-1641548568.png)
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