CodeGym /Blog Jawa /Acak /Jawa Nyekel Multiple Pangecualian
John Squirrels
tingkat
San Francisco

Jawa Nyekel Multiple Pangecualian

Sadurunge sinau babagan nyekel macem-macem pengecualian, sampeyan kudu ngerti babagan penanganan pengecualian dhasar ing Jawa. Obah maju, kita nganggep sampeyan wis menowo nyoba lan nyekel blok ing Jawa.

Napa kita butuh pirang-pirang blok nyekel ing Jawa?

Multiple pamblokiran nyekel ing Jawa digunakake kanggo nangani macem-macem jinis pangecualian. Sadurunge Java 7 diluncurake, kita butuh blok nyekel khusus kanggo nyekel pengecualian tartamtu. Iki nggawe blok kode sing berlebihan lan mula nyebabake pendekatan sing ora efisien. Deleng conto ing ngisor iki kanggo nyekseni pengecualian sing kejiret. Iki nggunakake blok nyekel sing kapisah kanggo macem-macem pengecualian.

Conto nggunakake Separate Catch Blocks

``````
import java.util.Arrays;

public class ExceptionHandler {

public static void main(String[] args) {

Integer[] colorsOfASpectrum = { 7, 6, 5, 4, 3, 2, 1, 0 };

try {

System.out.println("Total number of options on a dice are: " + Arrays.toString(colorsOfASpectrum));

// 		un-comment the following line to see "Index Out of Bounds Exception"
//		colorsOfASpectrum[10] = 7; // Index Out of Bounds Exception

System.out.println(colorsOfASpectrum[0] / 0);	// Arithmetic Exception

} catch (ArrayIndexOutOfBoundsException e) {
// This catch block executes in case of "Index Out of Bounds Exception"
System.out.println("Array Index Out Of Bounds Exception " + e);

} catch (ArithmeticException e) {
// This catch block executes in case of "Arithmetic Exception"
System.out.println("Arithmetic Exception " + e);
}

System.out.println("\n----Rest of the code executes here----");
}
}
``````

Output

Total werna ing spektrum yaiku: [7, 6, 5, 4, 3, 2, 1] Exception Encountered java.lang.ArithmeticException: / by zero ----Sisa kode dieksekusi ing kene----
Kaya sing sampeyan ngerteni, ing conto ing ndhuwur, blok sing beda dieksekusi nalika pengecualian dibuwang. Ana cara sing luwih efisien kanggo nyekel macem-macem pengecualian nggunakake blok kode sing padha kanggo nyekel pengecualian saka macem-macem jinis. Delengen conto ing ngisor iki.

Tuladha nggunakake Multiple Catch Block ing Jawa

``````
import java.util.Arrays;

public class MultiExceptionHandler {

public static void main(String[] args) {

Integer[] colorsOfASpectrum = { 7, 6, 5, 4, 3, 2, 1 };

try {

System.out.println("Total colors on a spectrum are: " + Arrays.toString(colorsOfASpectrum));

//		colorsOfASpectrum[10] = 7; // Index Out of Bounds Exception
System.out.println(colorsOfASpectrum[0] / 0); // Arithmetic Exception

} catch (ArrayIndexOutOfBoundsException | ArithmeticException e) {
// We don't need two different catch blocks for different kinds of exceptions
// Both exceptions will be handled using this multiple catch block

System.out.println("Exception Encountered " + e);
}
System.out.println("\n----Rest of the code executes here----");
}
}
``````

Output

Sampeyan bisa mbatalake komentar baris 13 kanggo nyithak kajaba jinis liyane.
Total werna ing spektrum yaiku: [7, 6, 5, 4, 3, 2, 1] Exception Encountered java.lang.ArithmeticException: / by zero ----Sisa kode dieksekusi ing kene----

Kesimpulan

Iki minangka ringkesan cepet babagan nggunakake java catch multiple exceptions block. Minangka tantangan, coba print wong tuwa lan pengecualian anak ing blok sing padha. Sampeyan disaranake sinau lan latihan kanggo tuwuh. Sugeng lan seneng sinau!
Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION