package com.yiibai.tutorial.exception;
public class HelloException {
public static void main(String[] args) {
System.out.println("Three");
// This division no problem.
int value = 10 / 2;
System.out.println("Two");
// This division no problem.
value = 10 / 1;
System.out.println("One");
// This division has problem, divided by 0.
// An error has occurred here.
value = 10 / 0;
// And the following code will not be executed.
System.out.println("Let's go!");
}
}
package com.yiibai.tutorial.exception;
public class HelloCatchException {
public static void main(String[] args) {
System.out.println("Three");
// This division no problem.
int value = 10 / 2;
System.out.println("Two");
// This division no problem.
value = 10 / 1;
System.out.println("One");
try {
// This division has problem, divided by 0.
// An error has occurred here.
value = 10 / 0;
// And the following code will not be executed.
System.out.println("Value =" + value);
} catch (ArithmeticException e) {
// The code in the catch block will be executed
System.out.println("Error: " + e.getMessage());
// The code in the catch block will be executed
System.out.println("Ignore...");
}
// This code is executed
System.out.println("Let's go!");
}
}
package com.yiibai.tutorial.exception.basic;
public class AgeException extends Exception {
public AgeException(String message) {
super(message);
}
}
TooYoungException.java
package com.yiibai.tutorial.exception.basic;
public class TooYoungException extends AgeException {
public TooYoungException(String message) {
super(message);
}
}
package com.yiibai.tutorial.exception.basic;
public class TooOldException extends AgeException {
public TooOldException(String message) {
super(message);
}
}
package com.yiibai.tutorial.exception.basic;
public class AgeUtils {
// This method checks the age.
// If age is less than 18, the method will throw an exception TooYoungException
// If age greater than 40, the method will throw an exception TooOldException
public static void checkAge(int age) throws TooYoungException,
TooOldException {
if (age < 18) {
// If age is less than 18, an exception will be thrown
// This method ends here.
throw new TooYoungException("Age " + age + " too young");
} else if (age > 40) {
// If age greater than 40, an exception will be thrown.
// This method ends here.
throw new TooOldException("Age " + age + " too old");
}
// If age is between 18-40.
// This code will be execute.
System.out.println("Age " + age + " OK!");
}
}
package com.yiibai.tutorial.exception.basic;
public class TryCatchDemo1 {
public static void main(String[] args) {
System.out.println("Start Recruiting ...");
// Check age
System.out.println("Check your Age");
int age = 50;
try {
AgeUtils.checkAge(age);
System.out.println("You pass!");
} catch (TooYoungException e) {
// Do something here ..
System.out.println("You are too young, not pass!");
System.out.println(e.getMessage());
} catch (TooOldException e) {
// Do something here ..
System.out.println("You are too old, not pass!");
System.out.println(e.getMessage());
}
}
}
package com.yiibai.tutorial.exception.basic;
public class TryCatchDemo2 {
public static void main(String[] args) {
System.out.println("Start Recruiting ...");
// Check age
System.out.println("Check your Age");
int age = 15;
try {
// Here can throw TooOldException or TooYoungException
AgeUtils.checkAge(age);
System.out.println("You pass!");
} catch (AgeException e) {
// If an exception occurs, type of AgeException
// This catch block will be execute
System.out.println("Your age invalid, you not pass");
System.out.println(e.getMessage());
}
}
}
package com.yiibai.tutorial.exception.basic;
public class TryCatchDemo3 {
public static void main(String[] args) {
System.out.println("Start Recruiting ...");
// Check age
System.out.println("Check your Age");
int age = 15;
try {
// Here can throw TooOldException or TooYoungException
AgeUtils.checkAge(age);
System.out.println("You pass!");
} catch (TooYoungException | TooOldException e) {
// Catch multi exceptions in one block.
System.out.println("Your age invalid, you not pass");
System.out.println(e.getMessage());
}
}
}
try {
// Do something here
} catch (Exception1 e) {
// Do something here
} catch (Exception2 e) {
// Do something here
} finally {
// Finally block is always executed
// Do something here
}
package com.yiibai.tutorial.exception.basic;
public class TryCatchFinallyDemo {
public static void main(String[] args) {
String text = "001234A2";
int value = toInteger(text);
System.out.println("Value= " + value);
}
public static int toInteger(String text) {
try {
System.out.println("Begin parse text: " + text);
// An Exception can throw here (NumberFormatException).
int value = Integer.parseInt(text);
return value;
} catch (NumberFormatException e) {
// In the case of 'text' is not a number.
// This catch block will be executed.
System.out.println("Number format exception " + e.getMessage());
// Returns 0 if NumberFormatException occurs
return 0;
} finally {
System.out.println("End parse text: " + text);
}
}
}
package com.yiibai.tutorial.exception.wrap;
public class Person {
public static final String MALE = "male";
public static final String FEMALE = "female";
private String name;
private String gender;
private int age;
public Person(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.yiibai.tutorial.exception.wrap;
// Gender Exception.
public class GenderException extends Exception {
public GenderException(String message) {
super(message);
}
}
package com.yiibai.tutorial.exception.wrap;
public class ValidateException extends Exception {
// Wrap an Exception
public ValidateException(Exception e) {
super(e);
}
}
package com.yiibai.tutorial.exception.wrap;
import com.yiibai.tutorial.exception.basic.AgeUtils;
public class ValidateUtils {
public static void checkPerson(Person person) throws ValidateException {
try {
// Check age.
// Valid if between 18-40
// This method can throw TooOldException, TooYoungException.
AgeUtils.checkAge(person.getAge());
} catch (Exception e) {
// If not valid
// Wrap this exception by ValidateException, and throw
throw new ValidateException(e);
}
// If that person is Female, ie invalid.
if (person.getGender().equals(Person.FEMALE)) {
GenderException e = new GenderException("Do not accept women");
throw new ValidateException(e);
}
}
}
package com.yiibai.tutorial.exception.wrap;
public class WrapperExceptionDemo {
public static void main(String[] args) {
// One participant recruitment.
Person person = new Person("Marry", Person.FEMALE, 20);
try {
// Exceptions may occur here.
ValidateUtils.checkPerson(person);
} catch (ValidateException wrap) {
// Get the real cause.
// May be TooYoungException, TooOldException, GenderException
Exception cause = (Exception) wrap.getCause();
if (cause != null) {
System.out.println("Not pass, cause: " + cause.getMessage());
} else {
System.out.println(wrap.getMessage());
}
}
}
}
package com.yiibai.tutorial.exception.runtime;
public class NullYiibaierExceptionDemo {
// For example, here is a method that can return null string.
public static String getString() {
if (1 == 2) {
return "1==2 !!";
}
return null;
}
public static void main(String[] args) {
// This is an object that references not null.
String text1 = "Hello exception";
// Call the method retrieves the string length.
int length = text1.length();
System.out.println("Length text1 = " + length);
// This is an object that references null.
String text2 = getString();
// Call the method retrieves the string length.
// NullYiibaierException will occur here.
// It is an exception occurs at runtime (type of RuntimeException)
// Javac compiler does not force you to use a try-catch block to handle it
length = text2.length();
System.out.println("Finish!");
}
}
// This is a null object.
String text2 = getString();
// Check to make sure 'Text2' are not null.
// Instead of using try-catch.
if (text2 != null) {
length = text2.length();
}
package com.yiibai.tutorial.exception.runtime;
public class ArrayIndexOfBoundsExceptionDemo {
public static void main(String[] args) {
String[] strs = new String[] { "One", "Two", "Three" };
// Access to the element has index 0.
String str1 = strs[0];
System.out.println("String at 0 = " + str1);
// Access to the element has index 5.
// ArrayIndexOfBoundsException occur here.
String str2 = strs[5];
System.out.println("String at 5 = " + str2);
}
}
if (strs.length > 5) {
String str2 = strs[5];
System.out.println("String at 5 = " + str2);
} else {
System.out.println("No elements with index 5");
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有