⑴ java自定义异常问题,请高手继续教育
package org.test.exception;public class ExceptionTest { public static String test(int num) throws MyException {
if (num < 0) {
throw new MyException("错误输入");
}
return "正常";
}
public static void main(String[] args) {
try {
System.out.println(test(-1));
} catch (MyException e) {
System.out.println(e.getMessage());
}
}
}class MyException extends Exception { private static final long serialVersionUID = 4027928832838793090L; private String message; public MyException(String message) {
this.message = message;
} @Override
public String getMessage() {
return this.message;
}}
⑵ java 如何自定义异常 用代码展示 真心靠谱
首先自定义一个异常类 public class ActionException extends Exception{ public String returnMessage; public ActionException(String returnMessage){ this.returnMessage = returnMessage; } public String getReturnMessage(){ return this.returnMessage; } 代码中如果用到这个自定义的异常类,这里的代码只是做了个演示 private void validate(int a,int b)throws ActionException{ if(a>b){ throw new ActionException("a > b"); } if(a<b){ throw new ActionException("a < b"); } } 业务逻辑代码中 public String process(){ try{ validate(a,b); }catch(ActionException ae){ System.out.println(ae.getReturnMessage()); } }
⑶ javaweb怎么自定义错误页面
不知道你的具体需求是什么,但通用的办法就是写一个try{}catch{},将可能会报错的程序用try{}包围,在catch{}里完成页面跳转,这样当程序报错时会自动跳转至自定义错误页面
⑷ Java中自定义抛异常的问题,求大神指点
1
Exception
是所有异常的顶级父类,只要出错就属于Exception,你放在自定义异常前面,不管出什么错,肯定会先catch到Exception,后面不管catch什么错误都永远不会执行到,所以编译直接不通过
2
当你传入一个字符的时候,而定义的是int类型,那该字符会直接转换为Unicode编码,a的
字符编码
是97,b是98,3/98,结果当然是0
⑸ java自定义异常
代码修改了,如下,有疑问可以追问:
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.JOptionPane;
publicclassNumExcp{
publicstaticvoidmain(String[]args){
NumExcpne=newNumExcp();
}
Frameframe;
Panelpanel;
TextFieldtf;
Buttonbtn;
NumExcp(){
frame=newFrame();
panel=newPanel();
btn=newButton("Check");
tf=newTextField(":");
ListenerAL=newListener();
btn.setEnabled(true);
frame.setVisible(true);
frame.setSize(400,400);
frame.setLocation(200,200);
panel.setSize(200,200);
panel.setVisible(true);
frame.add(panel);
frame.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
panel.add(tf);
panel.add(btn);
panel.setLayout(newFlowLayout(FlowLayout.CENTER));
btn.addActionListener(AL);
}
{
publicvoidactionPerformed(ActionEvente){
try{
intn=Integer.valueOf(tf.getText());
check(n);
}catch(Num_Exceptione1){
e1.e_message();
}
}
}
publicvoidcheck(intn)throwsNum_Exception{
if(n<10||n>100){
thrownewNum_Exception("请输入between10to100:");
}else{
tf.setText("Congrats!Yourinputiscorrect!");
}
}
classNum_ExceptionextendsException{
privateNum_Exception(Stringmessage){
super(message);
}
publicvoide_message(){
JOptionPane.showMessageDialog(null,"Invalidinput!"+this.getMessage(),"",JOptionPane.ERROR_MESSAGE);
}
}
}
⑹ 在JAVA中,用户程序如何自定义异常
/**
* class名:NewException
* class说明:在JAVA中,用户程序如何自定义异常?编程实现一个用户自定义异常
* @author Jr
*
*/
public class NewException extends Exception{
public NewException() {
super();
// TODO Auto-generated constructor stub
}
public NewException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public NewException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public NewException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
/**
* class名:ExceptionTest
* class说明:制造一个异常,来测试NewException类
* @author Jr
*
*/
public class ExceptionTest {
public static void main(String[] args) throws NewException {
System.out.println("开始正常");
try {
String str = null;
str.length();//将抛出异常
System.out.println("结束正常");
} catch (Exception e) {
e.printStackTrace();
throw new NewException("str.length()出现异常", e);
} finally{
}
}
}
自己定义一个异常只需继承(extends)Exception这个类或者Throwable这个类,然后继承父类的构造器就可以了。
⑺ java自定义异常问题
class ParameterNumberException extends Exception{
ParameterNumberException(){
super("参数个数异常!");}
public String toString(){
return "ParameterNumberException";}
}
class ParameterFormatException extends Exception{
ParameterFormatException(){
super("参数格式异常!");
public String toString(){
return "ParameterFomatException";}
}
public class NullPointer2 {
public static void main(String args[])throws ParameterNumberException,ParameterFormatException{
try{
if(args.length<2)
throw new ParameterNumberException();
else
throw new ParameterFormat();
}
catch(ParameterNumberException e){
System.out.println(e);
e.printStackTrace();}
catch(ParameterFomatException e){
System.out.println(e);
e.printStackTrace();}
}
}
⑻ JAVA中的自定义异常问题
//学生信息类
public class StudentInfo {
private int sno;
private String sname;
private int sage;
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
if(sage < 18||sage > 25){
try {
throw new MyException();
} catch (MyException e) {
System.out.println("年龄应该在18~25岁之间");
}
}else{
this.sage = sage;
}
}
}
//自定义异常类
public class MyException extends Exception{
public MyException(){
System.out.println("MyException");
}
}
//测试类
public class Test {
public static void main(String[] args) {
StudentInfo si = new StudentInfo();
si.setSage(33);
}
}
⑼ java中如何创建自定义异常
{
publicMyException()
{
super();
}
publicMyException(Stringmessage)
{
super(message);
}
publicstaticvoidmain(String[]args)
{
MyExceptionme=newMyException("自定义异常");
System.out.println("异常信息为:"+me.getMessage());
}
}