完了済
This commit is contained in:
6
Question16-Exception/.classpath
Normal file
6
Question16-Exception/.classpath
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21"/>
|
||||
<classpathentry kind="src" path="src/main/java"/>
|
||||
<classpathentry kind="output" path="build/classes"/>
|
||||
</classpath>
|
||||
23
Question16-Exception/.project
Normal file
23
Question16-Exception/.project
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Question16-Exception</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.wst.common.project.facet.core.builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,9 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
|
||||
org.eclipse.jdt.core.compiler.compliance=21
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=21
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<faceted-project>
|
||||
<installed facet="java" version="21"/>
|
||||
</faceted-project>
|
||||
6
Question16-Exception/build/classes/.gitignore
vendored
Normal file
6
Question16-Exception/build/classes/.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
/Question16_1.class
|
||||
/Question16_2.class
|
||||
/EmptyException.class
|
||||
/Question16_3.class
|
||||
/Question16_4.class
|
||||
/InvalidAgeException.class
|
||||
21
Question16-Exception/src/main/java/Question16_1.java
Normal file
21
Question16-Exception/src/main/java/Question16_1.java
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
public class Question16_1 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
|
||||
System.out.println(Divide(10,2));
|
||||
// System.out.println(Divide(10,0));
|
||||
try {
|
||||
System.out.println(Divide(10,0));
|
||||
} catch (Exception e) {
|
||||
System.out.println("0で割ることはできません");
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
public static int Divide(int num1,int num2) {
|
||||
return num1/num2;
|
||||
}
|
||||
|
||||
}
|
||||
41
Question16-Exception/src/main/java/Question16_2.java
Normal file
41
Question16-Exception/src/main/java/Question16_2.java
Normal file
@@ -0,0 +1,41 @@
|
||||
|
||||
public class Question16_2 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
|
||||
System.out.println(x2Multiple("10"));
|
||||
|
||||
// System.out.println(x2Multiple("abc"));
|
||||
try {
|
||||
System.out.println(x2Multiple("abc"));
|
||||
} catch(Exception e) {
|
||||
System.out.println("数値に変換できません");
|
||||
}
|
||||
|
||||
String test = "";
|
||||
// System.out.println("");
|
||||
try {
|
||||
if (test.length()==0) {
|
||||
throw new EmptyException("入力が空です");
|
||||
}
|
||||
} catch(EmptyException e) {
|
||||
System.out.println("入力が空です");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static int x2Multiple(String str) {
|
||||
return 2*Integer.parseInt(str);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class EmptyException extends Exception {
|
||||
|
||||
public EmptyException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
25
Question16-Exception/src/main/java/Question16_3.java
Normal file
25
Question16-Exception/src/main/java/Question16_3.java
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
public class Question16_3 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
|
||||
int[] nums = {10,20,30};
|
||||
getValue(nums,1);
|
||||
getValue(nums,5);
|
||||
|
||||
}
|
||||
|
||||
public static void getValue(int[] nums,int index) {
|
||||
|
||||
try {
|
||||
System.out.println(nums[index]);
|
||||
} catch(Exception e) {
|
||||
System.out.println("指定した位置にデータがありません");
|
||||
} finally {
|
||||
System.out.println("処理を終了します");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
31
Question16-Exception/src/main/java/Question16_4.java
Normal file
31
Question16-Exception/src/main/java/Question16_4.java
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
public class Question16_4 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
|
||||
age(20);
|
||||
age(-5);
|
||||
|
||||
}
|
||||
|
||||
public static void age(int num) {
|
||||
try {
|
||||
if (num<0) {
|
||||
throw new InvalidAgeException(num);
|
||||
} else {
|
||||
System.out.println("年齢は"+num+"です");
|
||||
}
|
||||
} catch(Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class InvalidAgeException extends Exception {
|
||||
public InvalidAgeException(int num) {
|
||||
System.out.println(num+"は不正な年齢です");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user