小编moh*_*abu的帖子

Try-catch-finally执行顺序似乎是随机的

我试图了解try-catch-finally执行流程的工作原理.Stack Overflow用户有一些关于其执行流程的解决方案.

一个这样的例子是:

try {
    // ... some code: A
} 
catch(...) {
    // ... exception code: B
} 
finally {
    // finally code: C
}
Run Code Online (Sandbox Code Playgroud)

代码A将被执行.如果一切顺利(即在执行A时没有异常被抛出),它将会转到finally,因此代码C将被执行.如果在执行A时抛出异常,那么它将转到B然后最终转到C.

但是,当我尝试时,我得到了不同的执行流程:

try {
    int a=4;
    int b=0;
    int c=a/b;
}
catch (Exception ex)
{
    ex.printStackTrace();
}
finally {
    System.out.println("common");
}
Run Code Online (Sandbox Code Playgroud)

我得到两个不同的输出:

第一输出:

java.lang.ArithmeticException: / by zero
at substrings.main(substrings.java:15)
lication.AppMain.main(AppMain.java:140)
common
Run Code Online (Sandbox Code Playgroud)

但是,当我第二次运行相同的程序时:

第二输出:

 common
    java.lang.ArithmeticException: / by zero
    at substrings.main(substrings.java:15)
Run Code Online (Sandbox Code Playgroud)

我应该从中得出什么结论?它是随机的吗?

java exception-handling exception

15
推荐指数
2
解决办法
1199
查看次数

如何克服ReactJS中的CORS问题

我试图在我的React Application中通过Axios进行API调用.但是,我在浏览器上遇到了这个CORS问题.我想知道我是否可以从客户端解决此问题,因为我内部没有任何API访问权限.附件是我的代码.

const response = axios({
   method: 'post',
   dataType: 'jsonp',
   url: 'https://awww.api.com',
   data: {
    'appToken':'',
    'request':{ 
        'applicationName':'ddfdf', 
        'userName':'jaime@dfd.com', 
        'password':'dfd', 
        'seasonIds':[1521ddfdfd5da02] 
     }
     }
    });
   return{
    type:SHARE_REVIEW,
    payload:'response'
 }
 }
Run Code Online (Sandbox Code Playgroud)

附件是我的WebPack.config.js

module.exports = {

entry: [
'./src/index.js'
 ],
output: {
  path: __dirname,
  publicPath: '/',
  filename: 'bundle.js'
},
module: {
 loaders: [{
  exclude: /node_modules/,
  loader: 'babel',
  query: {
    presets: ['react', 'es2015', 'stage-1']
  }
 },
 { test: /\.json$/, loader: "json-loader"}]
  },
 resolve: {
  extensions: ['', '.js', '.jsx']
 },
devServer: {
historyApiFallback: true,
contentBase: './' …
Run Code Online (Sandbox Code Playgroud)

javascript cors reactjs redux axios

9
推荐指数
5
解决办法
5万
查看次数

在 For 循环内执行递归

我试图理解为给定输入字符串编写排列的代码。

例如:输入字符串:123,输出:123,132,213,231,312,321。

下面粘贴的代码片段就是这样做的。

 public static void main(String args[]) {

    permuteString("", "123");
   }

public static void permuteString(String beginningString, String endingString) {
    if (endingString.length() <= 1)
        System.out.println(beginningString + endingString);
    else
        for (int i = 0; i < endingString.length(); i++) {
            try {
                 // System.out.println(i);
                String newString = endingString.substring(0, i) + endingString.substring(i + 1);
                permuteString(beginningString + endingString.charAt(i), newString);
            } catch (StringIndexOutOfBoundsException exception) {
                exception.printStackTrace();
            }
        }
Run Code Online (Sandbox Code Playgroud)

我对'i'for 循环中整数何时递增感到非常困惑,即从i=0 to 1. 我从第一次迭代中理解的一件事来看,'i'当它达到基本情况时,它会递增到 1

   if (endingString.length() <= 1)
        System.out.println(beginningString …
Run Code Online (Sandbox Code Playgroud)

java string recursion loops permutation

5
推荐指数
1
解决办法
2080
查看次数

实现java中接口和抽象类中存在的方法

我试图理解如果抽象类和接口中都存在方法会发生什么.下面,我发布了一个场景,让你清楚地了解我所指的是什么.

interface act
{
    void read();
}

abstract class enact
{
    void read() {
        System.out.println("hello");
    }
}

public class second extends enact implements act  
{
    public void read() {

        // does method read() correspond to  Interface or abstract class?
    }
}
Run Code Online (Sandbox Code Playgroud)

令人惊讶的是,编写此代码时没有编译错误.任何建议都会非常有帮助.

java oop abstract-class interface

5
推荐指数
1
解决办法
2334
查看次数

在ES6 javascript中更新现有对象数组

我是ES6 JavaScript的新手.我正在寻找一个场景,我可以更新来自Web服务的对象数组并将数据格式传递给表.

我正在使用ReactJS'rc-table'.

https://www.npmjs.com/package/rc-table
Run Code Online (Sandbox Code Playgroud)

要以rc-table格式显示我们的数据,我们需要在我们的数据中有一个属性键:"somevalue"来自后端.

我的数据采用以下格式:

{
 {
   Name:'Robert',
   City: 'Barcelona'
 },
 {
   Name: 'Marguaritte',
  City: 'Baltimore'

 }
}
Run Code Online (Sandbox Code Playgroud)

现在它将以rc-table格式显示,仅当对象具有唯一键属性时.

例如:

 {
 {
  Name:'Robert',
  City: 'Barcelona',
   Key:1
 },
 {
  Name: 'Marguaritte',
  City: 'Baltimore',
  Key:2

  }
 }
Run Code Online (Sandbox Code Playgroud)

我正在寻找用'key:1'和'key:2'更新我的对象.附上我的代码.任何帮助,将不胜感激.

javascript arrays json object ecmascript-6

3
推荐指数
1
解决办法
6037
查看次数

根据键(id)对perl数组数据结构中的键,值对进行排序

我试图在perl中对数组数据结构中存在的键值对进行排序.但是,当有多个条目时,我无法解析如何排序.

以下是我的代码:

  my @users = (
       {id => 1,  name => "Frank"},
       {id => 10, name => "Joe"},
       {id => 5,  name => "Paul"}
      );
Run Code Online (Sandbox Code Playgroud)

我希望输出按ID递增排序:

       {id => 1,  name => "Frank"},
       {id => 5,  name => "Paul"},
       {id => 10, name => "Joe"}
Run Code Online (Sandbox Code Playgroud)

以下是我的努力:

  use strict; 
  use warnings;
  use 5.010;
  my @users = 
  (
    {id => 1,  name => "Frank"},
    {id => 10, name => "Joe"},
   {id => 5,  name => "Paul"}
  );

 foreach my $name (keys %users) { …
Run Code Online (Sandbox Code Playgroud)

arrays sorting perl

2
推荐指数
1
解决办法
103
查看次数

在java脚本中无法在页面加载时显示两个警报按钮

我正在尝试编写一个代码,在加载页面时显示两个警报按钮.但是,我只能加载其中一个警报按钮(init2),即init1弹出窗口没有出现.下面附上我的代码.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"   
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html">
<title>Insert title here</title>
<script>
 // fname will be executed when the <body> tag loads
 function addOnLoad(fname){ 
    // Define this function

    window.onload=fname;
}

// init1 and init2 should be alerted when the body tag loads
addOnLoad(function (){alert('init1!'); });
addOnLoad(function (){ alert('init2!'); });
</script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

输出我得到:

在此输入图像描述

我不明白为什么init1的警报按钮没有打印的原因.我希望它们都打印出来.任何建议都会非常有用.

html javascript jquery

0
推荐指数
1
解决办法
63
查看次数