Found 4338 Articles for Java 8

How do you throw an Exception without breaking a for loop in java?

Maruthi Krishna
Updated on 12-Sep-2019 08:54:22

8K+ Views

Whenever an exception occurred in a loop the control gets out of the loop, by handling the exception the statements after the catch block in the method will get executed. But, the loop breaks.Example Live Demopublic class ExceptionInLoop{    public static void sampleMethod(){       String str[] = {"Mango", "Apple", "Banana", "Grapes", "Oranges"};          try {             for(int i=0; i

Can we to override a catch block in java?

Maruthi Krishna
Updated on 12-Sep-2019 08:46:56

856 Views

DescriptionWhen a piece of code in particular method throws an exception, and is handled using try-catch pair. If we are calling this method from another one and, the calling line is wrapped within try-catch pair. Now, how can I override the catch block by the catch block of the calling method.When a piece of code in a method throws an exception (compile time) we must either handle it by wrapping it within the try-catch pair or, throw it (postpone) to the calling method using the throws keyword else a compile time error occurs.In the following Java example the code in ... Read More

How to get Exception log from a console and write it to external file in java?

Maruthi Krishna
Updated on 12-Sep-2019 08:39:04

1K+ Views

There are several logging frame works available to log your data in to files. You can also define your own method.Example − Using I/O packageFollowing Java program has an array storing 5 integer values, we are letting the user to choose two elements from the array (indices of the elements) and performing division between them. We are wrapping this code in try block with three catch blocks catching ArithmeticException, InputMismatchException and, ArrayIndexOutOfBoundsException. In each of them we are invoking the writeToFile() method.This method accepts an exception object, and appends it to a file using the write() method of the Files ... Read More

Does code form Java finally block

Maruthi Krishna
Updated on 12-Sep-2019 08:26:37

124 Views

The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an Exception.The return statement in the method too does not stop the finally block from getting executed.ExampleIn the following Java program we are using return statement at the end of the try block still, the statements in the finally block gets executed. Live Demopublic class FinallyExample {    public static void main(String args[]) {       int a[] = new int[2];       try {          System.out.println("Access element three :" + a[3]);     ... Read More

What are the guide lines to be followed while using wildcards in Java?

Maruthi Krishna
Updated on 12-Sep-2019 08:22:22

100 Views

Instead of the typed parameter in generics (T) you can also use “?”, representing an unknown type. You can use a wild card as a −Type of parameter.Field.Local field.The only restriction on wilds cards is that you cannot it as a type argument of a generic method while invoking it.Java provides 3 types of wild cards namely upper-bounded, lower-bounded, un-bounded. There are two types of wildcards in Java −Upper-bounded wildcards − Upper bounds in wild cards is similar to the bounded type in generics. Using this you can enable the usage of all the subtypes of a particular class as a ... Read More

What are Unbounded wildcard w.r.t Generics method in Java?

Maruthi Krishna
Updated on 12-Sep-2019 08:17:35

1K+ Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.To define a generic class you need to specify the type parameter you are using in the angle brackets “” after the class name and you can treat this as datatype of the instance variable an ... Read More

What are lower-bounded wildcard w.r.t Generics method in Java?

Maruthi Krishna
Updated on 12-Sep-2019 08:09:36

246 Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.To define a generic class you need to specify the type parameter you are using in the angle brackets “” after the class name and you can treat this as datatype of the instance variable an ... Read More

What are upper-bounded wildcard w.r.t Generics method in Java?

Maruthi Krishna
Updated on 12-Sep-2019 08:01:45

279 Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.To define a generic class you need to specify the type parameter you are using in the angle brackets “” after the class name and you can treat this as datatype of the instance variable an ... Read More

Write data from/to a .csv file in java

Maruthi Krishna
Updated on 12-Sep-2019 06:52:18

1K+ Views

How to read/A library named OpenCSV provides API’s to read and write data from/into a.CSV file. Here it is explained how to read the contents of a .csv file using a Java program.Maven dependency    com.opencsv    opencsv    4.4    org.apache.commons    commons-lang3    3.9 The CSVReader class of the com.opencsv package represents a simple csv reader. While instantiating this class you need to pass a Reader object representing the file to be read as a parameter to its constructor. It provides methods named readAll() and readNext() to read the contents of a .csv fileUsing the readNext() ... Read More

Writing data from database to .csv file

Maruthi Krishna
Updated on 12-Sep-2019 06:47:26

4K+ Views

You can write data into a .csv file using the OpenCSV library and, you can communicate with MySQL database through a Java program using the mysql-java-connector.Maven dependencyThe following are the dependencies you need to include in your pom.xml file to write data to a .csv file from a database table.    com.opencsv    opencsv    4.4    org.apache.commons    commons-lang3    3.9    mysql    mysql-connector-java    5.1.6 Writing data to a CSV fileThe CSVWriter class of the com.opencsv package represents a simple CSV writer. While instantiating this class you need to pass a Writer object ... Read More

Advertisements