0% found this document useful (0 votes)
38 views2 pages

Contoh Program Throw

The document discusses a Java program that uses try/catch blocks to handle exceptions when taking the square root of a number. The program asks the user to input a number and then attempts to calculate its square root. If the number is negative, an ArithmeticException is thrown, and an error message is displayed. If the number is positive, the square root result is displayed. Finally, a thank you message is always displayed.

Uploaded by

Aldi Ibnu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views2 pages

Contoh Program Throw

The document discusses a Java program that uses try/catch blocks to handle exceptions when taking the square root of a number. The program asks the user to input a number and then attempts to calculate its square root. If the number is negative, an ArithmeticException is thrown, and an error message is displayed. If the number is positive, the square root result is displayed. Finally, a thank you message is always displayed.

Uploaded by

Aldi Ibnu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Contoh program throw

package JOP;
import javax.swing.JOptionPane;
public class tugas_asis {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "INI KALKULATOR AKAR");

int bilangan;
double akar;

bilangan=Integer.parseInt(JOptionPane.showInputDialog(null, "Masukkan Bilangan"));

try{
if(bilangan<0)
throw new ArithmeticException();
akar=Math.sqrt(bilangan);
try{
if(bilangan>=0)
throw new ArithmeticException();
}
catch(ArithmeticException e1){
JOptionPane.showMessageDialog(null, akar,"Nilai Akar",
JOptionPane.INFORMATION_MESSAGE);
}
}

catch(ArithmeticException e){
JOptionPane.showMessageDialog(null, "Bilangan harus lebih dari nol",
"PERINGATAN!!",JOptionPane.WARNING_MESSAGE);
}
finally{
JOptionPane.showMessageDialog(null, "TERIMA KASIH");
}
}

}
Output

You might also like