java大数相乘,希望出界抛异常。

问题描述

如题,我自己测试的时候,如果两个int型大数相乘,结果为负,求检测溢出的方法,并掷出异常的代码

解决方案

/** * Multiply two integers, checking for overflow. * * @param x a factor * @param y a factor * @return the product <code>x*y</code> * @throws ArithmeticException if the result can not be represented as an * int * @since 1.1 */ public static int mulAndCheck(int x, int y) { long m = ((long)x) * ((long)y); if (m < Integer.MIN_VALUE || m > Integer.MAX_VALUE) { throw new ArithmeticException("overflow: mul"); } return (int)m; } /** * Multiply two long integers, checking for overflow. * * @param a first value * @param b second value * @return the product <code>a * b</code> * @throws ArithmeticException if the result can not be represented as an * long * @since 1.2 */ public static long mulAndCheck(long a, long b) { long ret; String msg = "overflow: multiply"; if (a > b) { // use symmetry to reduce boundary cases ret = mulAndCheck(b, a); } else { if (a < 0) { if (b < 0) { // check for positive overflow with negative a, negative b if (a >= Long.MAX_VALUE / b) { ret = a * b; } else { throw new ArithmeticException(msg); } } else if (b > 0) { // check for negative overflow with negative a, positive b if (Long.MIN_VALUE / b <= a) { ret = a * b; } else { throw new ArithmeticException(msg); } } else { // assert b == 0 ret = 0; } } else if (a > 0) { // assert a > 0 // assert b > 0 // check for positive overflow with positive a, positive b if (a <= Long.MAX_VALUE / b) { ret = a * b; } else { throw new ArithmeticException(msg); } } else { // assert a == 0 ret = 0; } } return ret; }以上是commons-math-2.2的源码http://commons.apache.org/math/download_math.cgi
解决方案二:
int c = a*b;if(c/b!=a){ System.out.println("溢出");} 这是一个大概的思路,楼主可以尝试一下

时间: 2024-08-07 03:43:53

java大数相乘,希望出界抛异常。的相关文章

在aix里部署了tomcat6.0.29,java.net.URLConnection.getInputStream().close()抛异常

问题描述 在aix(aix是ibm定制的unix)里部署了tomcat5,tomcat6,里面有相同的java程序,其中有一段是StringcheckUrlStr="http://192.168.0.1:8080/aos/index.html";java.net.URLcheckUrl=newjava.net.URL(checkUrlStr);java.net.URLConnectionconnection=checkUrl.openConnection();connection.se

捕获Java线程池执行任务抛出的异常

捕获Java线程池执行任务抛出的异常Java中线程执行的任务接口java.lang.Runnable 要求不抛出Checked异常, public interface Runnable { public abstract void run(); } 那么如果 run() 方法中抛出了RuntimeException,将会怎么处理了? 通常java.lang.Thread对象运行设置一个默认的异常处理方法: java.lang.Thread.setDefaultUncaughtExceptionH

ftp程序在read的地方卡死了,等服务器断开后才抛异常java.net.socketException

问题描述 ftp程序在read的地方卡死了,等服务器断开后才抛异常java.net.socketException,设置了ftp.setReadTimeout(120000);//等待2*60*1000毫秒发生阻塞抛出异常后仍然无效,求大神指教,为什么会这样,怎么解决.以下贴出ftp远程下载服务器端代码privatestaticvoidgetFilefromServer(FtpClientftp,OutputStreamout,StringfileName)throwsException{byt

java通过抛异常来返回提示信息

结论:如果把通过抛异常的方式得到提示信息,可以使用java.lang.Throwable中的构造函数: public Throwable(String message) { fillInStackTrace(); detailMessage = message; } 或 public Throwable(String message, Throwable cause) { fillInStackTrace(); detailMessage = message; this.cause = caus

java中重写方法必须抛出和原方法一样的异常或者不抛异常

class A {     public void method throws IOException(){} } // 错误 class B {     public void method throws FileNotFoundException(){} } // 错误 class C {     public void method throws Exception(){} } // 错误 class D {     public void method throws IOExceptio

java中double类型运算结果异常的解决方法_java

问题: 对两个double类型的值进行运算,有时会出现结果值异常的问题.比如: System.out.println(19.99+20); System.out.println(1.0-0.66); System.out.println(0.033*100); System.out.println(12.3/100); 输出: 39.989999999999995 0.33999999999999997 3.3000000000000003 0.12300000000000001 解决方法: J

初学java反射的问题 一直抛出classnotfoundexception

问题描述 初学java反射的问题 一直抛出classnotfoundexception public class TestReflection { public static void main(String[] args) { try{ String str = ""T""; Class c = Class.forName(str); c.newInstance(); } catch (ClassNotFoundException e) { e.printStac

mp4parser音视频合成抛异常Expected Start Word 0x7ff

问题描述 mp4parser音视频合成抛异常Expected Start Word 0x7ff 我用mp4parser合成音视频的时候,总是抛这个异常,换成AACTrackImpl也是这个问题,请问这个怎么解决? mp3文件跟AAC文件都是可以正常播放的 java.io.IOException: Expected Start Word 0x7ff at com.googlecode.mp4parser.authoring.tracks.MP3TrackImpl.readMP3Header(MP3

mysql数据库-一个javaWeb项目,为什么一到用户、Email注册校验时,就抛异常

问题描述 一个javaWeb项目,为什么一到用户.Email注册校验时,就抛异常 log4j:WARN No appenders could be found for logger (com.mchange.v2.log.MLog). log4j:WARN Please initialize the log4j system properly. 2015-9-23 19:40:18 org.apache.catalina.core.StandardWrapperValve invoke 严重: