1030自主学習
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>
|
||||
1
Question16-Exception/.gitignore
vendored
Normal file
1
Question16-Exception/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build/
|
||||
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>
|
||||
19
Question16-Exception/src/main/java/Question16_1.java
Normal file
19
Question16-Exception/src/main/java/Question16_1.java
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
public class Question16_1 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
try {
|
||||
int result1 = divide(10, 2);
|
||||
System.out.println(result1);
|
||||
int result2 = divide(10, 0);
|
||||
}catch(ArithmeticException e){
|
||||
System.out.println("0で割ることはできません");
|
||||
}
|
||||
}
|
||||
|
||||
public static int divide(int num1, int num2) {
|
||||
return num1 / num2;
|
||||
}
|
||||
|
||||
}
|
||||
32
Question16-Exception/src/main/java/Question16_2.java
Normal file
32
Question16-Exception/src/main/java/Question16_2.java
Normal file
@@ -0,0 +1,32 @@
|
||||
|
||||
public class Question16_2 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
ExceptionHandling("10");
|
||||
ExceptionHandling("abc");
|
||||
ExceptionHandling("");
|
||||
}
|
||||
|
||||
public static void ExceptionHandling(String scr) {
|
||||
try {
|
||||
int result = convertandtwice(scr);
|
||||
System.out.println(result);
|
||||
}catch(NumberFormatException e) {
|
||||
System.out.println("数値に変換できません");
|
||||
}catch(IllegalArgumentException e) {
|
||||
System.out.println("入力が空です");
|
||||
}
|
||||
}
|
||||
|
||||
public static int convertandtwice(String str) {
|
||||
if(str.isEmpty()) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
int num = Integer.parseInt(str);
|
||||
num *= 2;
|
||||
return num;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
30
Question16-Exception/src/main/java/Question16_3.java
Normal file
30
Question16-Exception/src/main/java/Question16_3.java
Normal file
@@ -0,0 +1,30 @@
|
||||
|
||||
public class Question16_3 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
int[] nums = new int[3];
|
||||
nums[0] = 10;
|
||||
nums[1] = 20;
|
||||
nums[2] = 30;
|
||||
|
||||
Check(nums, 1);
|
||||
Check(nums, 5);
|
||||
}
|
||||
|
||||
public static int getArrayValue(int[] num1, int num2 ) {
|
||||
return num1[num2];
|
||||
}
|
||||
|
||||
public static void Check(int[] num1, int num2 ) {
|
||||
try {
|
||||
int result = getArrayValue(num1, num2);
|
||||
System.out.println(result);
|
||||
}catch(ArrayIndexOutOfBoundsException e) {
|
||||
System.out.println("指定した位置にデータがありません");
|
||||
}finally{
|
||||
System.out.println("処理を終了します");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
25
Question16-Exception/src/main/java/Question16_4.java
Normal file
25
Question16-Exception/src/main/java/Question16_4.java
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
public class Question16_4 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
|
||||
try{
|
||||
int num1 = ageCheck(20);
|
||||
System.out.println("年齢は" + num1 + "です");
|
||||
|
||||
int num2 = ageCheck(-5);
|
||||
System.out.println("年齢は" + num2 + "です");
|
||||
}catch(testException e) {
|
||||
System.out.println(e.getMessage()+ "は不正な年齢です");
|
||||
}
|
||||
}
|
||||
|
||||
public static int ageCheck(int num) throws testException {
|
||||
if(num < 0) {
|
||||
throw new testException("num");
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
}
|
||||
6
Question16-Exception/src/main/java/testException.java
Normal file
6
Question16-Exception/src/main/java/testException.java
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
public class testException extends Exception {
|
||||
public testException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user