throw exception java code example
Example 1: c# throw new exception
static void CopyObject(SampleClass original)
{
if (original == null)
{
throw new System.ArgumentException("Parameter cannot be null", "original");
}
}
Example 2: throw io exception java
public static void foo() throws IOException {
throw new IOException("error message");
}
public static void main(String[] args) {
try {
foo();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
Example 3: php throw exception
function inverse($x) {
if (!$x) {
throw new Exception('Division by zero.');
}
return 1/$x;
}
Example 4: in java how to throw exception from function
public void doChangePin(int oldPin, int pin) throws Exception {
if (oldPin == pinCode) {
pinCode = pin;
} else {
throw new Exception("some message");
}
}
Example 5: java how to throw exception
public class ThrowException{
public static void main(String [] args) throws Exception{
throw new Exception("Errmessage");
}
}
Example 6: throwing exceptions java
public class ThrowExample {
static void checkEligibilty(int stuage, int stuweight){
if(stuage<12 && stuweight<40) {
throw new ArithmeticException("Student is not eligible for registration");
}
else {
System.out.println("Student Entry is Valid!!");
}
}
public static void main(String args[]){
System.out.println("Welcome to the Registration process!!");
checkEligibilty(10, 39);
System.out.println("Have a nice day..");
}
}