1. 程式人生 > >Spring 中異常處理方法的總結

Spring 中異常處理方法的總結

前言

在程式設計過程中,我們總是會遇到各種各樣的一樣,受檢異常和非受檢異常,也可以對這些異常進行重寫或者擴充套件,總而言之,這就涉及到一個處理異常的問題。

好的異常處理方式既容易配置又可以保持使用端的友好互動,更為重要的是在出現問題的時候快速的幫助我們定位問題。

事實上,對程式碼的理解很總要,這樣你就知道什麼時候應該丟擲什麼異常了,比如陣列越界,檔案過大,棧越界,連線超時這些標準異常,等等。當然,在什麼時候丟擲什麼異常不是本文的重點,網上同類的文章很多,傳送門Java 基礎 積累-不斷更新:異常分類:Error,Exception,RuntimeException以及其子類,其他異常

本文主要著重於異常的丟擲方式和捕獲方式。

再一個,對於以介面對外提供服務的專案與以頁面對外提供服務的專案側重點也是不一樣的。前者需要在遇到異常時繼續返回接口出參,只不過是內容有所不同,而以頁面互動

的在出現異常時,則可能是跳轉到了不同的頁面並且給予提示資訊。

最後,異常的處理包括異常的丟擲和處理,丟擲異常是一個,捕獲異常和接下來的處理是另一個。

(1)丟擲異常——捕獲異常——跳轉頁面

(2)丟擲異常——捕獲異常——接口出參

程式碼

基礎工作

(1)自定義的異常類

package com.bestcxx.stu.fileup.exception;

/**
 * 自定義的異常類
 * @author WuJieJecket
 *
 */
@SuppressWarnings("serial")
public class MyException extends Exception {
	public MyException(String string) {
		super(string);
	}

}

(2)頁面等前端檔案的位置-檢視解析配置-關注頁面基礎位置WEB-INF/views

applicationContext-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:util="http://www.springframework.org/schema/util"  
    xmlns:orm="http://www.springframework.org/schema/orm"   
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd  
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd  
                        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd  
                        http://www.springframework.org/schema/orm http://www.springframework.org/schema/orm/spring-orm-4.3.xsd  
                        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd  
    ">
    <!-- 配置註解驅動 -->  
    <mvc:annotation-driven/>
    
    <!-- 定義預設訪問 -->  
    <mvc:view-controller path="/" view-name="forward:home"/>
    
    <!-- 處理靜態資源 -->
	<mvc:resources location="/WEB-INF/css/" mapping="/css/**" />
	<mvc:resources location="/WEB-INF/js/" mapping="/js/**" />
	
    <!-- 配置檢視解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>  
        <property name="contentType" value="text/html"/>        
        <property name="prefix" value="/WEB-INF/views/"/>  
        <property name="suffix" value=".jsp"/>  
    </bean>
    
   
</beans>

開始了

異常的丟擲

(1)區域性方法丟擲

	/**
	 * 異常處理測試
	 * @return
	 */
	@GetMapping("exception")
	public String exception() throws Exception{
		if(true){
			throw new MyException("報錯");			
		}
		
		return "success";
	}

(2)攔截器丟擲-類加配置檔案

MyInterceptor.java

package com.bestcxx.stu.fileup.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.bestcxx.stu.fileup.exception.MyException;

public class MyInterceptor implements HandlerInterceptor {
	private long maxSize;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		System.out.println(this.getClass()+" preHandle開始:"+System.currentTimeMillis());
		System.out.println(this.getClass()+" 自定義攔截器訪問前:"+maxSize);
		 if(request!=null && ServletFileUpload.isMultipartContent(request)) {
	            ServletRequestContext ctx = new ServletRequestContext(request);
	            long requestSize = ctx.contentLength();
	            if (requestSize > maxSize) {
	            	System.out.println(this.getClass()+" preHandle結束:"+System.currentTimeMillis()+" 檔案太大主動丟擲異常"); 
	            	//throw new MaxUploadSizeExceededException(maxSize);
	                throw new MyException(maxSize+"檔案太大超了");
	            }
	        }
		System.out.println(this.getClass()+" preHandle結束:"+System.currentTimeMillis());
		return true;
		
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		System.out.println(this.getClass()+" postHandle開始:"+System.currentTimeMillis());
		System.out.println(this.getClass()+" 自定義攔截器訪問中:"+maxSize);
		System.out.println(this.getClass()+" postHandle結束:"+System.currentTimeMillis());
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println(this.getClass()+" afterCompletion開始:"+System.currentTimeMillis());
		System.out.println(this.getClass()+" 自定義攔截器訪問後:"+maxSize);
		System.out.println(this.getClass()+" afterCompletion結束:"+System.currentTimeMillis());

	}

	public void setMaxSize(long maxSize) {
		this.maxSize = maxSize;
	}
	
	

}


applicationContext-intercepter.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:util="http://www.springframework.org/schema/util"  
    xmlns:orm="http://www.springframework.org/schema/orm"   
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd  
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd  
                        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd  
                        http://www.springframework.org/schema/orm http://www.springframework.org/schema/orm/spring-orm-4.3.xsd  
                        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd  
    ">
    <!-- 攔截器配置 -->
     <mvc:interceptors>
     	<mvc:interceptor>
     		<mvc:mapping path="/**"/>
     		<bean class="com.bestcxx.stu.fileup.intercepter.MyInterceptor">
     			<!-- <property name="maxSize" value="5242880"></property> --><!-- 5mb -->
     			<property name="maxSize" value="5000"></property>
     		</bean>
     	</mvc:interceptor>
     </mvc:interceptors>
    
</beans>

(3)過濾器丟擲-類加web.xml

(4)Spring 為檔案上傳大小限制單獨提供的異常-MaxUploadSizeExceededException

需要增加配置檔案

applicationContext-file.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:util="http://www.springframework.org/schema/util"  
    xmlns:orm="http://www.springframework.org/schema/orm"   
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd  
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd  
                        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd  
                        http://www.springframework.org/schema/orm http://www.springframework.org/schema/orm/spring-orm-4.3.xsd  
                        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd  
    ">
    <!--檔案上傳-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
		<!-- <property name="maxUploadSize"><value>1</value></property> -->
		<!-- <property name="maxUploadSize"><value>5242880</value></property> --><!-- 設定上傳檔案的最大尺寸為 5mb=5*1024kb=5*1024*1024b -->
		<property name="maxInMemorySize"><value>1</value></property><!-- 小於該尺寸的檔案不生成臨時檔案 -->
		<property name="defaultEncoding"><value>UTF-8</value></property><!-- 預設上傳格式為utf-8 -->
		<property name="resolveLazily"><value>true</value></property><!-- 延遲處理異常-將異常交予 controller 處理 -->
	</bean> 
</beans>

異常的捕獲

(1)區域性方法捕獲-與丟擲異常的方法處於同一個controller類-可介面可頁面

這裡是捕獲異常後跳轉到一個頁面

/**
	 * 捕獲本類的異常
	 * 優先順序比全域性異常高
	 * 針對頁面請求的異常處理,預設返回error.jsp
	 * 一個類僅允許存在一個 @ExceptionHandler 標註的方法
	 * @param ex
	 * @param request
	 * @return
	 */
	@ExceptionHandler(Exception.class)
	public ModelAndView handlePageException(Exception ex, WebRequest request) {
	request.setAttribute("msg","異常捕獲:"+this.getClass()+" "+ex.getMessage(), RequestAttributes.SCOPE_REQUEST);
	ModelAndView mav = new ModelAndView("error");
	mav.addObject("ex", ex);
	return mav;
	}
	
	
	
 

	/**
	 * 異常處理測試
	 * @return
	 */
	@GetMapping("exception")
	public String exception() throws Exception{
		if(true){
			throw new MyException("報錯");			
		}
		
		return "success";
	}

WEB-INF/error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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; charset=UTF-8">
<title>error.jsp</title>
</head>
<body>
	<h1> ${msg} </h1>
	<h1> ${ex} </h1>
</body>
</html>

如果是返回一個介面的話

需要注意的是,一個類只能有一個方法被 @Exceptionhandler 註釋

本文使用@ResponseBody 做實體與json轉化。其他更便捷方法請參考Spring json和物件的自動轉化

/**
	 * 捕獲本類的異常
	 * 優先順序比全域性異常高
	 * 針對介面調動的異常處理
	 * 一個類僅允許存在一個 @ExceptionHandler 標註的方法
	 * @param ex
	 * @param request
	 * @return
	 */
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public Object handleRestfulException(Exception ex, WebRequest request) {
		request.setAttribute("msg","異常捕獲:"+this.getClass()+" "+ex.getMessage(), RequestAttributes.SCOPE_REQUEST);
		ModelAndView mav = new ModelAndView("error");
		mav.addObject("ex", ex);
		HashMap map=new HashMap();
		map.put("msg", "非 ModelAndView 的實體或者字串");
		return map;
	}

(2)全域性捕獲

有三種方式,以為只需要一種就可以了,所以將他們統一羅列出來

applicationContext-exception.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:util="http://www.springframework.org/schema/util"  
    xmlns:orm="http://www.springframework.org/schema/orm"   
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd  
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd  
                        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd  
                        http://www.springframework.org/schema/orm http://www.springframework.org/schema/orm/spring-orm-4.3.xsd  
                        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd  
    ">
   <!-- 方式一 -->
   <!-- 全域性異常捕獲-針對頁面訪問 -->
    <!-- <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
	     定義預設的異常處理頁面
	    <property name="defaultErrorView" value="error"/>
	    定義異常處理頁面用來獲取異常資訊的變數名,也可不定義,預設名為exception 
    	<property name="exceptionAttribute" value="ex"/>
    	設定日誌輸出級別,不定義則預設不輸出警告等錯誤日誌資訊     
        <property name="warnLogCategory" value="WARN"></property> 
        預設HTTP狀態碼     
        <property name="defaultStatusCode" value="200"></property>     
         
    	對於需要特殊處理的異常
    	<property name="exceptionMappings">     
             <props>     
                 <prop key="java.lang.Exception">error</prop>     
                 <prop key="java.lang.Error">error</prop>
             </props>     
         </property>     
         <property name="statusCodes">     
             <props>     
                 <prop key="error">500</prop>/views/error.jsp
             </props>     
         </property>
	</bean> -->
    
    <!-- 方式二 -->
	<!-- Exception 類捕獲-針對頁面或者介面訪問-->
   <!-- <context:component-scan base-package="com.bestcxx.stu.fileup.exception.ExceptionResolver"/>  -->
    
    <!-- 方式三 -針對頁面或者介面訪問-->
    <!-- 全域性異常處理器只要你實現了HandlerExceptionResolver介面,這個類就是一個全域性異常處理器-->  
    <bean class="com.bestcxx.stu.fileup.exception.GlobalExceptionResolver"></bean>
    
</beans>


方式一是針對頁面訪問的

方式二和方式三可以是頁面也可以是介面(本例是頁面處理)

且方式二和方式三需要單獨提供處理類,在類內部寫如下的內容,返回為null即可,網上也有返回json型別的view

@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		if(ex instanceof NoLoginException){
			PrintWriter writer=null;
			try {
				writer = response.getWriter();
				writer.write(((NoLoginException)ex).toString());
				writer.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				if(writer!=null){
					writer.close();
				}
			}
				
		}
		return null;
	}

方式二ExceptionResolver.java

package com.bestcxx.stu.fileup.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.ModelAndView;

/**
 * 全域性異常配置
 * @author Administrator
 *
 */
@ControllerAdvice
public class ExceptionResolver {
	
	@ExceptionHandler(RuntimeException.class)
    public ModelAndView handlerRuntimeException(RuntimeException ex){
		if(ex instanceof MaxUploadSizeExceededException){
			return new ModelAndView("error").addObject("msg", "檔案太大!");			
		}
		return new ModelAndView("error").addObject("msg", "未知錯誤:"+ex);	
    }
	
	@ExceptionHandler(Exception.class)
    public ModelAndView handlerMaxUploadSizeExceededException(Exception ex){
		if(ex instanceof Exception){
			return new ModelAndView("error").addObject("msg", ex);			
		}
		
		return new ModelAndView("error").addObject("msg", "未知錯誤:"+ex);	
		
    }

}


方式三GlobalExceptionResolver.java

package com.bestcxx.stu.fileup.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

public class GlobalExceptionResolver implements HandlerExceptionResolver {

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		ModelAndView modelAndView=new ModelAndView();  
		 System.out.println("異常資訊輸出:"+ex.toString());
		//如果是檔案太大
		if (ex instanceof MaxUploadSizeExceededException) {
            long maxSize = ((MaxUploadSizeExceededException) ex).getMaxUploadSize();
            System.out.println(this.getClass()+" 上傳檔案太大,不能超過" + maxSize/1024/1024 + "mb"); // 列印錯誤資訊
            
            //將錯誤資訊傳到頁面  
            modelAndView.addObject("msg",this.getClass()+" 出現異常:"+"上傳檔案太大,不能超過" + maxSize/1024/1024 + "mb"+ex.getMessage()); 
            
            //指向到錯誤介面  
            modelAndView.setViewName("error");  
            return modelAndView;
        } 
		
		
        if(ex instanceof MyException){ //自定義異常
        	 modelAndView.addObject("msg","自定義異常:"+ex.getMessage());  
        }else{  
            //如果該 異常型別不是系統 自定義的異常,構造一個自定義的異常型別(資訊為“未知錯誤”)。  
        	modelAndView.addObject("msg","未知異常:"+ex.getMessage());  
        }  
		
        //將錯誤資訊傳到頁面  
        modelAndView.addObject("msg","出現異常:"+ex.getMessage());  
          
        //指向到錯誤介面  
        modelAndView.setViewName("error");  
          
        return modelAndView;
	}

}

相關推薦

Spring 異常處理方法總結

前言 在程式設計過程中,我們總是會遇到各種各樣的一樣,受檢異常和非受檢異常,也可以對這些異常進行重寫或者擴充套件,總而言之,這就涉及到一個處理異常的問題。 好的異常處理方式既容易配置又可以保持使用端的友好互動,更為重要的是在出現問題的時候快速的幫助我們定位問題。 事實上

Java異常處理方法總結

Java中常用的異常處理有3中方式: 1.try {} catche{} try...catch 適合處理程式設計師可以控制和使用者輸入有誤的情況,把認為可能會出現異常的程式碼放到try後面的{},異常後執行的程式碼放到catche的{}裡面,catche的{}後面的語句正

Perl異常處理方法總結

程式指令碼在執行過程中,總會碰到這樣那樣的問題,我們會預知一些問題併為其準備好處理程式碼,而有一些不能預知。好的程式要能儘可能多的處理可能出現的異常問題,本文就總結了一些方法來解決這些異常,當然perl在這個處理了不及其它同類語言,但也不會差到那裡。在開始前,我

java異常處理機制 throw拋出自定義業務邏輯異常 throws繼續拋出 catch捕獲後會自動繼續拋向調用方法

異常處理機制 ... cep super finally sta exc ace 避免 package com.swift; public class Exception_TestC { public static void main(String[] arg

JAVA專案常用的異常處理情況總結

1. java.lang.nullpointerexception 這個異常大家肯定都經常遇到,異常的解釋是"程式遇上了空指標",簡單地說就是呼叫了未經初始化的物件或者是不存在的物件,這個錯誤經常出現在建立圖片,呼叫陣列這些操作中,比如圖片未經初始化,或者圖片建立時的路徑錯誤等等。對陣列操作中出現空指標

關於JAVA專案的常用的異常處理情況總結

1. JAVA異常處理 在面向過程式的程式語言中,我們可以通過返回值來確定方法是否正常執行。比如在一個c語言編寫的程式中,如果方法正確的執行則返回1.錯誤則返回0。在vb或delphi開發的應用程式中,出現錯誤時,我們就彈出一個訊息框給使用者。 通過方法的返回值我們並不能獲得錯誤的詳細資訊。可能因為方法由

Spring Boot & Spring MVC 異常處理的N種方法

預設行為 根據Spring Boot官方文件的說法: For machine clients it will produce a JSON response with details of the error, the HTTP status and the excep

強烈推薦:Java程式設計過程正確的異常處理方法

  Java程式設計過程中的異常處理是一個很常見的話題,幾乎任何一門介紹性的Java課程都會提到異常處理。不過,我認為很多人其實並沒有真正掌握正確處理異常情況的方法和策略,最多也就瞭解個大概,知道點概念。 首先我來解釋一些java異常處理中必須搞清楚的定義和機制吧。Java語

Spring異常的統一的處理方式

在具體的SSM專案開發中,由於Controller層為處於請求處理的最頂層,再往上就是框架程式碼的。 因此,肯定需要在Controller捕獲所有異常,並且做適當處理,返回給前端一個友好的錯誤碼。 不過,Controller一多,我們發現每個Controller裡都有大量重複的、冗餘的異常處

【詳】JAVA異常分類以及異常處理方法之間的區別以及聯絡

異常在日長開發中就像看見地鐵裡邊的人頭一樣的,放眼望去,齊刷刷的一片,各種異常。分佈範圍比較廣泛,很多人看到異常,就這表情 JAVA中異常: NullPointerException 空指標異常

spring3.1整合quartz時,spring依賴注入無法注入到quartz的job處理方法

定時任務類 package cn.customercard.controller; import java.util.Date; import javax.servlet.ServletContextEvent; import javax.serv

Python異常處理,梳理&總結

python提供了兩個非常重要的功能來處理python程式在執行中出現的異常和錯誤。你可以使用該功能來除錯python程式。 1.python標準異常: 異常名稱 描述 BaseException 所有異常的基類 SystemExit 直譯器請求退出 Keyb

Java的常用異常處理方法

  作為一個Java程式設計師,你至少應該能夠找出兩個問題。但是,如果你不能找出全部六個問題,請繼續閱讀本文。   本文討論的不是Java異常處理的一般性原則,因為這些原則已經被大多數人熟知。我們要做的是分析各種可稱為“反例”(anti-pattern)的違背優秀編碼規範的常見壞習慣,幫助讀者熟悉這些典型的反

Java工作的併發問題處理方法總結

Java工作中常見的併發問題處理方法總結 =========== 好像挺久沒有寫部落格了,趁著這段時間比較閒,特來總結一下在業務系統開發過程中遇到的併發問題及解決辦法,希望能幫到大家

Spring MVC異常處理實例

bsp ips etag label 視圖 uri _id integer ive 以下內容引用自http://wiki.jikexueyuan.com/project/spring/mvc-framework/spring-exception-handling-examp

【基礎】C#異常處理總結

ember -s 有效 理解 dac 十進制 reference bsp msil 一、異常處理的理解? 異常處理是指程序在運行過程中,發生錯誤會導致程序退出,這種錯誤,就叫做異常。 因此處理這種錯誤,就稱為異常處理。 二、異常處理如何操作? C# 異常處

關於js數組方法總結

filter foreach 默認 tin ffffff 關於 調用 index reducer 數組在筆試中經常會出現的面試題,javascript中的數組與其他語言中的數組有些不同,為了方便之後數組的方法學習,整理了關於數組的操作方法 數組創建 JavaScript中創

day15——異常常用形式、異常處理方法、raise關鍵字的使用

nco 尚未實現 對象 縮進 lower 會有 tran ise 通知 異常既是一個事件,該事件會在程序執行過程中發生,影響了程序的正常執行。 一般情況下,在Python無法正常處理程序時就會發生一個異常。異常是Python對象,表示一個錯誤。當Python腳本發生

JAVA循環刪除list元素的方法總結

tor 循環 toolbar i++ 使用 修改 log span .get  印象中循環刪除list中的元素使用for循環的方式是有問題的,但是可以使用增強的for循環,然後今天在使用時發現報錯了,然後去科普了一下,再然後發現這是一個誤區。下面就來講一講。。伸手黨可直接跳

spring boot 異常處理(轉)

integer stat 全部 control nts custom ref default turn spring boot在異常的處理中,默認實現了一個Embedde