導航:首頁 > 編程語言 > java自定義錯誤

java自定義錯誤

發布時間:2022-04-19 08:36:29

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());
}
}

閱讀全文

與java自定義錯誤相關的資料

熱點內容
壓縮包解碼器下載 瀏覽:130
愛旅行的預備程序員 瀏覽:111
安卓qq瀏覽器怎麼轉換到ios 瀏覽:292
不同編譯器的庫可以調用嗎 瀏覽:455
灰度信託基金加密 瀏覽:421
宿遷程序員兼職網上接單 瀏覽:924
電視編譯器怎麼設置 瀏覽:276
手機如何解壓漢字密碼的壓縮包 瀏覽:701
為什麼很多程序員愛用vim 瀏覽:828
安卓手機怎麼連接寶華韋健音響 瀏覽:555
12星座製作解壓球 瀏覽:867
java調用oracle數據 瀏覽:827
怎麼在伺服器上上傳小程序源碼 瀏覽:304
空中加油通達信指標公式源碼 瀏覽:38
分卷解壓只解壓了一部分 瀏覽:760
php網站自動登錄 瀏覽:705
合肥凌達壓縮機招聘 瀏覽:965
怎麼找到文件夾的圖標 瀏覽:237
linuxc編程pdf百度雲 瀏覽:192
會計pdf下載 瀏覽:835