Experimental nine: the exception is thrown, catch and handle

Experimental procedure:

package experiment nine;

import java.util.Scanner;

public class Point {
static int x;
static int y;
Point(int x,int y){
Point.x=x;
Point.y=y;
}
@SuppressWarnings("serial")
public static void main(String[] args) {
try {
@SuppressWarnings({ "unused", "resource" })
Scanner shuru=new Scanner(System.in);
System.out.println("请输入一个点:");
int h=shuru.nextInt();
int a=shuru.nextInt();

@SuppressWarnings("unused")
Point s=new Point(h,a);
if(x>=0&&y>=0) {
System.out.println("x为:"+Point.x+"y为:"+Point.y);
}
else {
throw new Exception() {
public String toString() {
return "无效参数";
}
};
}
}
catch(Exception e){
System.out.println(e.toString());
}
finally {
System.out.println("程序结束");

}
}

}

package experiment nine;

import java.util.Scanner;

public class Rectangle {
static int length;
static int width;
Rectangle(Point point1,int length,int width){
Rectangle.length=length;
Rectangle.width=width;
}
@SuppressWarnings("serial")
public static void main(String[] args) {
try {
@SuppressWarnings("resource")
Scanner shuru=new Scanner(System.in);

System.out.println ( "Enter a point:");
int shuru.nextInt H = ();
int shuru.nextInt A = ();

System.out.println ( "Please enter the rectangular length:");
int shuru.nextInt B = ();

System.out.println ( "Please enter the width of a rectangle:");
int shuru.nextInt C = ();
Point P1 Point new new = (H, A);
@SuppressWarnings ( "unused")
the Rectangle the Rectangle new new m = (P1 , B, C);
IF (length> = 0 && width> = 0) {
System.out.println ( "this is a length" + Rectangle.length + "width" + Rectangle.width + "rectangular");
}
the else {
the throw new new Exception () {
public String toString () {
return "invalid parameter";
}
};
}
}
the catch (Exception E) {
System.out.println (e.toString ());
}
the finally {
the System.out. println ( "end of program");

}
}

}

 

package experiment nine;

import java.util.Scanner;

public class Polygon {
Polygon(Point[] points) {

}
@SuppressWarnings ( "Serial")
public static void main (String [] args) {
@SuppressWarnings ( "Resource")
Scanner Scanner shuru new new = (the System.in);
System.out.println ( "Enter n");
n-shuru.nextInt = int ();
int Point [] = new new int [n-];
int I;
for (I = 0; I <n-; I ++) {
System.out.println ( "Please enter polygon" + i + "points");
int = h1 of shuru.nextInt ();
int shuru.nextInt A1 = ();
@SuppressWarnings ( "unused")
point P2 point new new = (h1 of, A1);
}
the try {
// point S new new Point = (-1,2);
IF (Point.length> =. 3)
{
System.out.println ( "which may constitute a polygon");
}
else {
throw new Exception() {
String toString public () {
return "Invalid parameter";
}
};
}
}
the catch (Exception E) {
System.out.println (e.toString ());
}
the finally {
System.out.println ( "end of program") ;

}
}
}

 

 

package experiment nine;

import java.util.Scanner;

public class Triangle {
@SuppressWarnings("unused")
private static final Point Point = null;
public Point point2,point3;
protected double a,b,c;
public Triangle(Point p1,Point p2,Point p3) {
//super("三角形",p1);
this.point2=p2;
this.point3=p3;

}
public Triangle() {
}
@SuppressWarnings("serial")
public static void main(String[] args) {

try {
@SuppressWarnings("resource")
Scanner shuru=new Scanner(System.in);

System.out.println ( "Enter a point:");
int shuru.nextInt H = ();
int shuru.nextInt A = ();
Point P1 Point new new = (H, A);

System.out.println ( "Please enter a point:");
int = h1 of shuru.nextInt ();
int shuru.nextInt A1 = ();
point P2 point new new = (h1 of, A1);

System.out.println ( "Please enter a point: ");
int shuru.nextInt H2 = ();
int shuru.nextInt A2 = ();
Point new new Point P3 = (H2, A2);
@SuppressWarnings (" unused ")
Triangle Triangle new new S = (P1, P2, P3 );
Double G = (h1 of-H) / (A1-A);
Double P = (h1 of-H2) / (A2-A1);
! IF (P = G) {
System.out.println ( "three points may form a triangle ");
}
The else {
the throw new new Exception () {
public String toString () {
return "Invalid parameter";
}
};
}
}
The catch (Exception E) {
System.out.println (e.toString ());
}
the finally {
System.out.println ( "End of program");

}
}

}

Experimental experience: I learned through this experiment to an exception thrown, caught and processed. Program by try {} catch an exception, try if an exception occurs, run the program will be interrupted, throwing "objects generated exception classes." try {} thrown exception object will enter the catch () determines whether abnormality in parentheses want to capture, if it is, will enter the catch {} for exception handling within the code block, the program continues to run down, there may be multiple a catch () {}, i.e., there may be a plurality of abnormal objects, according to exception handling. finally {} procedures are performed regardless of whether the captured exception, generally close to the object.

Guess you like

Origin www.cnblogs.com/Java199-wfx/p/10927143.html