gpt4 book ai didi

java - 抛出异常问题

转载 作者:太空宇宙 更新时间:2023-11-04 11:30:53 25 4
gpt4 key购买 nike

我很难抛出 IllegalStateForMatrixException。如果有人可以帮助我,我将不胜感激。这是我的教授告诉我要做的,但实际上我仍然遇到问题。这是他给我的提示......

当调用具有 0 个边的 getNumberOfEdges 时,应引发 IllegalStateForMatrixException。当调用具有 0 个边的 getNumberOfEdges 时,应引发 IllegalStateForMatrix。您需要做的就是检查矩阵没有正值。如果它没有抛出 IllegalStateForMatrix 异常。

您还可以将 boolean 值设置为 false,并在调用 createMatrix 时将其设置为 true。这是一种检查 createMatrix 是在 addEdge 之前还是之后调用的方法。然后检查 addEdge 就可以抛出异常。

class AdjacencyGraph extends Graph {

private ArrayList nodes = new ArrayList();
private int noEdges = 0;

/**
* Adjacency matrix.
*/
public int[][] adjM;

/**
* Boolean set to false, should be set true in create matrix.
*/
private boolean b = false;

/**
* Adjacency Matrix with integers.
*/
@Override
void createAdjacencyMatrix() {
adjM = new int[nodes.size()][nodes.size()];
int i;
int j;
for (i = 0; i < nodes.size(); i++) {
for (j = 0; j < nodes.size(); j++) {
adjM[i][j] = -1;
}
b = true;
}
}

/**
* Adds the node.
*
* @param nodeName
*/
@Override
@SuppressWarnings("unchecked")
void addNode(String nodeName) {
nodes.add(nodeName);
}

/**
* Adds the edge.
*
* @param fromNode
* @param toNode
* @param weight
* @throws ElementNotFoundException
* @throws IllegalStateForMatrixException
*/
@Override
void addEdge(String fromNode, String toNode, int weight)
throws ElementNotFoundException, IllegalStateForMatrixException {
{
try {
if ( b == false)
throw new IllegalStateForMatrixException("Exception");
int i;
int j;
for (i = 0; i < nodes.size(); i++) {
if (nodes.get(i).equals(fromNode)) {
break;
}
}
if (i == nodes.size()) {
throw new ElementNotFoundException("Exception");
}

for (j = 0; j < nodes.size(); j++) {
if (nodes.get(j).equals(toNode)) {
break;
}
}
if (j == nodes.size()) {
throw new ElementNotFoundException("Exception");
}

adjM[i][j] = weight;
adjM[j][i] = weight;
noEdges++;
}
catch (ElementNotFoundException e) {
System.out.println("Exception found");
}
catch (IllegalStateForMatrixException e) {
System.out.println("Exception found");
}
}
}

/**
* Returns the number of nodes.
*
* @return number of nodes
*/
@Override
int getNumberOfNodes() {
return nodes.size();
}

/**
* Returns the number of edges
*
* @return number of edges
* @throws IllegalStateForMatrixException
*/
@Override
int getNumberOfEdges() throws IllegalStateForMatrixException {
if (nodes.size() <= 0)
throw new IllegalStateForMatrixException("Error");
return noEdges;
}

/**
* Returns the highest degree node.
*
* @return highest degree node.
* @throws ElementNotFoundException
* @throws IllegalStateForMatrixException
*/
@Override
public String getHighestDegreeNode()
throws ElementNotFoundException, IllegalStateForMatrixException {

int i;
int ansIndex = 0;
int j;
int ansCount = 0;
for (i = 0; i < nodes.size(); i++) {
int k = 0;
for (j = 0; j < nodes.size(); j++) {
if (adjM[i][j] != -1) {
k++;
}
}
if (k > ansCount) {
ansCount = k;
ansIndex = i;
}
}
return (String) nodes.get(ansIndex);
}

/**
* Cost of the edge between nodes.
*
* @param fromNode
* @param toNode
* @return returns -1
* @throws ElementNotFoundException
* @throws IllegalStateForMatrixException
*/
@Override
int costOfEdgeBetween(String fromNode, String toNode)
throws ElementNotFoundException, IllegalStateForMatrixException {
try {
int i;
int j;
for (i = 0; i < nodes.size(); i++) {
if (nodes.get(i).equals(fromNode)) {
break;
}
}
if (i == nodes.size()) {
throw new ElementNotFoundException("Exception");
}

for (j = 0; j < nodes.size(); j++) {
if (nodes.get(j).equals(toNode)) {
break;
}
}
if (j == nodes.size()) {
throw new ElementNotFoundException("Exception");
}

return adjM[i][j];
}
catch (ElementNotFoundException e) {
System.out.println("Exception found");
}
return -1;
}

/**
*
* @param fromName
* @param toName
* @return false
* @throws ElementNotFoundException
* @throws IllegalStateForMatrixException
*/
@Override
boolean hasPathBetween(String fromName, String toName)
throws ElementNotFoundException, IllegalStateForMatrixException {
try {
ArrayStack<Integer> st = new ArrayStack<Integer>();
int[] visited = new int[nodes.size()];
int i;
int j;
int start;
int end;
for (i = 0; i < nodes.size(); i++) {
visited[i] = 0;
}

for (i = 0; i < nodes.size(); i++) {
if (nodes.get(i).equals(fromName)) {
break;
}
}
start = i;

for (j = 0; j < nodes.size(); j++) {
if (nodes.get(j).equals(toName)) {
break;
}
}
end = j;
st.push(start);
visited[start] = 1;
while (st.isEmpty() != true) {
i = st.pop();
if (i == end) {
return true;
}
for (j = 0; j < nodes.size(); j++) {
if (adjM[i][j] != -1 && visited[j] == 0) {
visited[j] = 1;
st.push(j);
}
}
}
return false;
}
catch (Exception e) {
System.out.println("Exception found");
}
return false;
}

/**
* The number of Isolated points
*
* @return ans
* @throws IllegalStateForMatrixException
*/
@Override
int numIsolatedPoints() throws IllegalStateForMatrixException {
int i;
int j;
int ans = 0;
for (i = 0; i < nodes.size(); i++) {
for (j = 0; j < nodes.size(); j++) {
if (adjM[i][j] != -1) {
break;
}
}
if (j == nodes.size()) {
ans++;
}
}
return ans;
}

/**
* Inclusiveness is the percentage of points in the graph that are not
* isolated
*
* @return ((float)i)/nodes.size()
* @throws IllegalStateForMatrixException
*/
@Override
float inclusiveness() throws IllegalStateForMatrixException {
int i = nodes.size() - numIsolatedPoints();
return ((float) i) / nodes.size();
}

/**
* Density of matrix
*
* @return (2*(float)noEdges)/(nodes.size()*(nodes.size()-1))
* @throws IllegalStateForMatrixException
*/
@Override
float density() throws IllegalStateForMatrixException {
return (2 * (float) noEdges) / (nodes.size() * (nodes.size() - 1));
}

/**
* Print out the adjacency matrix
*/
void print() {
System.out.println(adjM);
}

}

最佳答案

How do I throw and catch the IllegalStateForMatrix properly. Where would I properly catch the exception? –

这里 IllegalStateForMatrixException 表示在对象上调用了一个操作,但该对象不处于接受此操作所需先决条件的状态。

您想要获得许多边,但没有矩阵?这没有任何意义。

这是一个重要的问题,因此您必须引发异常直到引发异常的方法的调用者

除此之外,这些方法还声明抛出 IllegalStateForMatrixException
因此,如果检查了异常(不是 RuntimeException 的子异常),则客户端必须处理它。

在您的代码中,有两个方法抛出异常。
在这里,你可以做好事:

@Override
int getNumberOfEdges() throws IllegalStateForMatrixException {
if (nodes.size() <= 0)
throw new IllegalStateForMatrixException("Error");
return noEdges;
}

您将异常抛出给必须处理它的客户端。

那里:

void addEdge(String fromNode, String toNode, int weight)
throws ElementNotFoundException, IllegalStateForMatrixException {
try {
if ( b == false)
throw new IllegalStateForMatrixException("Exception");
}
...
}
catch (IllegalStateForMatrixException e) {
System.out.println("Exception found");
}
...
}

当您在方法中引发异常时,您不允许将异常传播给调用者,但您也捕获了它。
与第一种情况一样,您不需要 try/catch 语句。

关于java - 抛出异常问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43812460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com