try {
System.out.print("整数データを1つ入力して下さい ");
・・・・・・・・
}
catch (IOException ignored) {}
public static void main(String args[]) throws IOException
/****************************/
/* 例外処理 */
/* coded by Y.Suganuma */
/****************************/
import java.io.*;
public class Test {
public static void main (String[] args)
{
try {
sq(1.0, 2.0);
sq(-1.0, 2.0);
sq(-1.0, -2.0);
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
static void sq(double x, double y) throws Exception
{
double z;
if (x < 0.0 && y < 0.0)
throw new Exception("両方とも負");
else {
if (x < 0.0 || y < 0.0)
throw new Exception("片方が負");
}
z = Math.sqrt(x+y);
System.out.println(z);
}
}
/****************************/
/* 例外処理 */
/* coded by Y.Suganuma */
/****************************/
import java.io.*;
public class Test {
public static void main (String[] args)
{
try {
sq(1.0, 2.0);
sq(-1.0, 2.0);
sq(-1.0, -2.0);
}
catch (Negative ex)
{
ex.message();
}
}
static void sq(double x, double y) throws Negative
{
double z;
if (x < 0.0 && y < 0.0)
throw new Negative("両方とも負",x, y);
else {
if (x < 0.0 || y < 0.0)
throw new Negative("片方が負", x, y);
}
z = Math.sqrt(x+y);
System.out.println(z);
}
}
class Negative extends Exception {
Negative(String str, double x, double y) {
System.out.println(str + " " + x + " " + y);
}
void message() {
System.out.println(" データを修正してください");
}
}
| 戻る |