解释代码,里面两个方法分别是干什么的:public class NegativeException extends Exception { public NegativeException() { super("the length of a side is negative "); // TODO Auto-generated constructor stub } public NegativeException(String message) { super(message); // TODO Auto-generated constructor stub } }
时间: 2024-02-07 12:02:05 浏览: 27
这是一个自定义异常类 `NegativeException`,继承了 `Exception` 类。其中有两个构造方法:
- `NegativeException()`:无参构造方法,调用了父类 `Exception` 的构造方法,并传入字符串参数 `"the length of a side is negative "`,意为“边长为负数”。
- `NegativeException(String message)`:有参构造方法,同样调用了父类 `Exception` 的构造方法,并传入参数 `message`,该参数为异常信息的具体描述。
这个自定义异常类的作用是用于在程序中检测到某边长为负数时,抛出此异常。程序员可以在捕获这个异常时,根据异常信息的具体描述,进行相应的处理。
相关问题
android如何向servlet传两个String类型数据再接收该servlet传回的数据完成登录功能完整代码
好的,这是一个关于Android与Java Web的交互的问题,需要用到HttpURLConnection类来发送HTTP请求,下面是完整的代码实现:
1. Android客户端代码
```
public class LoginActivity extends AppCompatActivity {
private EditText etUsername;
private EditText etPassword;
private Button btnLogin;
private TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etUsername = findViewById(R.id.et_username);
etPassword = findViewById(R.id.et_password);
btnLogin = findViewById(R.id.btn_login);
tvResult = findViewById(R.id.tv_result);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("http://your_server_address/loginServlet");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
String data = "username=" + URLEncoder.encode(username, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(data.getBytes().length));
conn.setDoOutput(true);
conn.getOutputStream().write(data.getBytes());
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
final StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
tvResult.setText(sb.toString());
}
});
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
tvResult.setText("请求失败");
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
});
}
}
```
2. Java Web服务器端代码
```
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
// 在这里处理登录逻辑,如验证用户名密码是否正确,返回结果
String result = "登录成功";
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(result);
}
}
```
注意:上述代码中的“your_server_address”需要替换成你自己的服务器地址。
编写一个FullStackException类,它继承Exception类。捕获该异常时,可以通过该类继承来的getMessage方法获取栈的深度。
好的,以下是FullStackException类的代码:
```java
public class FullStackException extends Exception {
public FullStackException(String message) {
super(message);
}
public int getStackDepth() {
return getStackTrace().length;
}
}
```
这个类继承了Exception类,并添加了一个名为getStackDepth的方法,用于获取栈的深度。当抛出FullStackException异常时,可以使用getMessage方法获取异常信息,也可以使用getStackDepth方法获取栈的深度。例如:
```java
public class Test {
public static void main(String[] args) {
try {
test();
} catch (FullStackException e) {
System.out.println(e.getMessage());
System.out.println("Stack depth: " + e.getStackDepth());
}
}
public static void test() throws FullStackException {
try {
test1();
} catch (Exception e) {
throw new FullStackException("Stack overflow");
}
}
public static void test1() {
test2();
}
public static void test2() {
test3();
}
public static void test3() {
test4();
}
public static void test4() {
// 循环调用自身,造成栈溢出
test4();
}
}
```
在这个例子中,test4方法会循环调用自身,最终造成栈溢出异常。在test方法中,catch块捕获异常并抛出FullStackException异常,并传入异常信息。在main方法中,通过getMessage方法和getStackDepth方法获取异常信息和栈的深度。输出结果如下:
```
Stack overflow
Stack depth: 7
```