简体   繁体   中英

Passing an Integer with Exception in Java

I have following exception

public class MyException extends SecurityException{
      public MyException( String s ) { super( s ) ; }
}

where SecurityException is java.lang.SecurityException .

whenever I throw MyException("Message"); , I can get the message.

Now, in MyException I want to pass an integer value with MyException and access that integer where I am catching this MyException .

How do I do that ?

public class MyException extends SecurityException{
  private int i;
  public MyException(String s, int i) { 
     super(s);
     this.i = i;
  }

  public int getI() { return i; }
}

You can specify a member variable in MyException class and use it at catch point. Something like -

public class MyException extends SecurityException{
  private Integer value;
  public MyException( Integer n, String s ) { 
       super(s) ;
       value = n;
  }
  public Integer getValue() {
       return value;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM